Thread: multiple include of header file gives compile errors

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    72

    multiple include of header file gives compile errors

    Hi All

    I need some guidance with header files because I can't solve the following compile error:

    Code:
    make
    gcc -I./lib  -c file1.c  -o file1.o
    gcc -I./lib  -c file2.c  -o file2.o
    gcc -I./lib  -c file.c  -o file.o
    gcc -Wall -g -fast -o stats file1.o file2.o file.o -L./lib -lmyconfig 
    ld: duplicate symbol _default_keys in file.o and file1.o
    collect2: ld returned 1 exit status
    make: *** [stats] Error 1
    Both file.c and file1.c include libmyconfig.h and file.c includes file1.h.
    libmyconfig.h looks (simplyfied) like

    Code:
    void check_config_file(char *file) ;
    int load_config_file(char *filename) ;
    struct CONFIG
     {
        int  *var1 ;
        char *var2 ;
     } ;
    char *default_keys[]   =  {"var1", "var2"} ;
    char *default_values[] =  {"10", "20"} ;
    So, default_keys is included twice. I tried to solve this by adding ifndef .... endif:
    Code:
    #ifndef BLA
    #define BLA
    .....
    #endif
    Unfortunately it didn't change anything Any suggestions why?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because those two lines don't belong in a header file. If all these other files need to know about that variable, then you should put "extern char * default_keys[]" in the header, and one file needs to define them.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Define default keys and default_values in a source file instead. If their declaration is needed, declare them as extern in the header.
    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

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    72
    Ok, make sense!, but then I have one last question, if I have a header file like

    Code:
    void check_config_file(char *file) ;
    int load_config_file(char *filename) ;
    struct CONFIG
     {
        int  *var1 ;
        char *var2 ;
     } ;
    why would one use #ifndef FOO ..... #endif, because it will not give compile errors!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try to include that header twice in the same source file, or more likely in practice, include that header in two other headers, then include those two other headers in the same source 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

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    72
    check, thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. to #include or not to #include
    By krygen in forum C++ Programming
    Replies: 9
    Last Post: 12-25-2004, 12:06 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM