multiple include of header file gives compile errors

This is a discussion on multiple include of header file gives compile errors within the C Programming forums, part of the General Programming Boards category; Hi All I need some guidance with header files because I can't solve the following compile error: Code: make gcc ...

  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,185
    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
    21,794
    Define default keys and default_values in a source file instead. If their declaration is needed, declare them as extern in the header.
    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

  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
    21,794
    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.
    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

  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, 04: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-24-2004, 11:06 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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