multiple declarations of functions errors

This is a discussion on multiple declarations of functions errors within the C Programming forums, part of the General Programming Boards category; i am developing a program which has a number of threads and many functions previously the program had a header ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    14

    multiple declarations of functions errors

    i am developing a program which has a number of threads and many functions
    previously the program had a header file and a main file so as a result the header file was becoming bigger and bigger.

    so i decided to split its functionality among multiple .c files and .h files but when i do so i get a lot of
    multiple declaration of function x sort errors

    how can i overcome these . is there any tutorial on this..
    /*****
    One can't let go and still win
    ******/

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    You should then use header guards.

    e.g.

    #ifndef _FILENAME_H
    #define _FILENAME_H

    /* function declarations */

    #endif
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Use this method while you are learning the in's and out's.

    Create one header file, this will contain ALL function prototypes and any enums or typedefs you might have. Includes this header in all your .c files.

    so you header might look like this:
    Code:
    #ifndef MYHEADER_H_
    #define MYHEADER_H_
    
    int foo (void);
    void bar (int *, char);
    
    #endif
    Then your .c files would have
    Code:
    #include "myheader.h"
    /* ... */
    One thing I like to do is to add comments in the header that specify which file the function is in. EX:
    Code:
    #ifndef MYHEADER_H_
    #define MYHEADER_H_
    
    int foo (void);               /* In foo.c */
    void bar (int *, char);   /* In bar.c */
    
    #endif
    Hopefully this helps.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > Create one header file, this will contain ALL function prototypes
    One per .c file is much simpler to manage

    The interface for foo.c is specified by foo.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    IMO For someone that is just starting off one .h is easier. Then you start moving into have a seperate .h for each .c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  2. Multiple functions
    By rculley1970 in forum C Programming
    Replies: 13
    Last Post: 11-25-2006, 03:42 PM
  3. Multiple Object Declarations
    By sean in forum C# Programming
    Replies: 3
    Last Post: 09-26-2004, 10:42 PM
  4. Writing to single file in multiple functions
    By alvision in forum C Programming
    Replies: 12
    Last Post: 08-22-2004, 08:15 PM
  5. Declaring multiple functions
    By samuel in forum C++ Programming
    Replies: 1
    Last Post: 10-22-2001, 12:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21