Thread: Need Help Linking Files

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    Need Help Linking Files

    Note: I'm constricted to what i describe my problem as. Meaning alternate ways of doing it is not possible.

    I'm trying to seperate some functions into different files.

    Header File A has some typedef
    Header File B has function prototypes
    Commong File has some commonly used functions and defines etc.

    Source File A is the main program
    Source File B-Z are the functions. (1 Function = 1 File)

    Some of the functions require the common file and some require the typedefs.
    But i also need the function file to be able to be compilied seperately without errors.

    I've tried a lot of things but i always seem to get some type of error: multiple decleration, undeclared etc. Whats the best way of doing this?

    Very much appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by deltagods
    Header File B has function prototypes
    Commong File has some commonly used functions and defines etc.
    As in the common header file contains the declarations (not definitions) of commonly used functions and macros, while the declarations of all the other functions are contained in header file B?

    Quote Originally Posted by deltagods
    Some of the functions require the common file and some require the typedefs.
    But i also need the function file to be able to be compilied seperately without errors.
    Presumably you would include the common header file in all the source files, and include the other two headers where necessary.
    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
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Quote Originally Posted by laserlight View Post
    As in the common header file contains the declarations (not definitions) of commonly used functions and macros, while the declarations of all the other functions are contained in header file B?


    Presumably you would include the common header file in all the source files, and include the other two headers where necessary.
    Common header file has a boolean type declaration. And yes header file b has the function prototypes.

    I tried including the common where its needed and the other 2 header files but i get multiple declaration errors.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Can you give some specifics about what you are getting multiple declaration errors with. A function? A variable?

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Common.h -> Boolean Declaration

    Header FileA
    Code:
    #define   A        20 
    #define   B        60  
    
    typedef enum state { DEAD, ALIVE } State;
    typedef State Grid[A+2][B+2];
    Header FileB -> Includes Common & Header FileA. Has Function Declarations

    Main -> Includes Header FileB. Includes All the Function Files

    Function FileA -> Include Common & Header FileA
    Function FileB-Z -> Include Header FileA


    With that setup i have an error for multiple definition of a function in common and the functions in each of the function files.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by deltagods
    Main -> Includes Header FileB. Includes All the Function Files
    That sounds wrong. The main source file should not be including other source files. They should be compiled separately and then linked. Either that, or the main source file does include the other source files, but then you only compile the main source file. (The latter is quite a rare approach, though in some cases a library author may combine source files in this way to produce a single source file for distribution.)
    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

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Yes most of the errors are gone. Makes a lot more sense now.

    But i'm still stuck with the error on the function in common.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by deltagods
    But i'm still stuck with the error on the function in common.
    Post the updated source of your common header file.
    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

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef enum boolean { FALSE, TRUE } Boolean;
    
    void Error(char *);
    
    void Warning(char *);
     /* Error: report program error.
    
    
    Pre:    s points to the message to be printed.
    Post:  The function prints the message and terminates the program.
     */
    void Error(char *s)
    {
        fprintf(stderr, "%s\n", s);
        exit(1);
    }

    Error is
    multiple definition of `Error'

    Only thing is common is used for is the boolean. Which is used as a return value for one of the functions.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You defined the Error function in the common header. Don't do that. Move that function definition to a source file.

    Also, your header files should have header inclusion guards.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  2. html link question about linking to files for download
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-01-2004, 01:27 PM
  3. Linking Error Whenever I Tried To Use Header Files
    By javacvb in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2003, 11:46 AM
  4. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM