Thread: multiple declarations of functions errors

  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
    28,413
    You should then use header guards.

    e.g.

    #ifndef _FILENAME_H
    #define _FILENAME_H

    /* function declarations */

    #endif
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

  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, 01:00 PM
  2. Multiple functions
    By rculley1970 in forum C Programming
    Replies: 13
    Last Post: 11-25-2006, 04: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