Tell gcc to not include a header more than once?

This is a discussion on Tell gcc to not include a header more than once? within the C Programming forums, part of the General Programming Boards category; Hi there, here's a really quick question: say I have some files: linalg.h Code: typedef struct { ... } evector; ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    35

    Tell gcc to not include a header more than once?

    Hi there,
    here's a really quick question:

    say I have some files:

    linalg.h
    Code:
    typedef struct { 
        ... 
    } evector;
    void copyMatrix2D( double **dst, double **src, int nrows, int ncols );
    ...
    electron.h
    Code:
    #include linalg.h
    typedef struct {
            evector r;
            ...
    } diffuser;
    void moveDmcElectron( evector *epos, gsl_rng *r, const double sigma );
    ...
    test.c
    Code:
    #include electron.h
    extern inline void moveDmcElectron( evector *epos, gsl_rng *r, const double sigma );
    #include linalg.h
    extern inline void copyMatrix2D( double **dst, double **src, int nrows, int ncols );
    ...
    Basically when I try to compile (I compiled the various bits seperately, then try to link when compiling test.c) - it complains that I have two definitions of 'evector' (a typedef'd struct).

    Is there a way to get around this? for eg telling the compiler not to include a header that it's already included (even if it was indirectly?)

    There has to be a way around this, because people would do this all the time surely? (ie include two headers, one of which also includes the other one, but for different reasons.

    cheers
    VIM + gcc + beer... does life get any better?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In your header files, use include guards:
    Code:
     
     #ifndef MYHEADER
      #define MYHEADER
      
      /*
       Prototypes etc here 
       */
      
      #endif
    Sample here: (yes, it c++, but you'll get the idea)
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Without really reading your code, I'm gonna give you 2 suggestions:

    1. Add:
    PHP Code:
    #pragma once 
    above the header.

    or...

    2. Add a
    PHP Code:
    #ifndef __HEADER_FILE_HERE_
    #define __HEADER_FILE_HERE_ 
    at the top of the header file, and a
    PHP Code:
    #endif __HEADER_FILE_HERE_ 
    as the very last line of the header file.
    ** you can change the text after define's **

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    oh! I actually tried that but it didn't seem to help - I got the exact same error message

    Perhaps I did it wrong somehow...

    cheers for the help
    VIM + gcc + beer... does life get any better?

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    does that pragma statement only apply to the line directly beneath it?
    VIM + gcc + beer... does life get any better?

  6. #6
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by eccles
    does that pragma statement only apply to the line directly beneath it?
    No, it goes for the whole file from top to bottom.

    It just basically says "include this file only once in file XXX.c(pp)"

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >#ifndef __HEADER_FILE_HERE_
    Names with leading underscores are reserved for the implementation. Your code is dangerous at best.

    >#pragma once
    Last I heard, that was an obsolescent feature.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Keep in mind that pragma statement is compiler specific. It's best to use the include guards as mentioned above.

  9. #9
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Prelude
    >#ifndef __HEADER_FILE_HERE_
    Names with leading underscores are reserved for the implementation. Your code is dangerous at best.

    >#pragma once
    Last I heard, that was an obsolescent feature.
    For #1, I had a feeling it was "wrong", but I was doing it off the top of my head since I haven't done C/++ in quite a while.

    As for #2, I knew it was obsolete, but it does work.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    aaahhh wait a sec - now I get what you mean...
    hehe I had been trying to put an include statement in the ifndef...

    cool! so once it's loaded once, any further attempts will do nothing. brilliant I love it! thanks so much
    VIM + gcc + beer... does life get any better?

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Sample:

    Code:
        /* main.c */'
        #include <stdio.h>
        
        #include "head1.h"
        #include "head2.h"
        
        int main(void)
        {
          return(0);
        }
    Code:
        /* head1.h */
        #ifndef HEAD1_H
        #define HEAD1_H
        
        typedef struct mystruct
        {
          int i;
        } mystruct_t;
        
        #endif
    Code:
        /* head2.h */
        #ifndef HEAD2_H
        #define HEAD2_H
        
        #include "head1.h"
        
        
        
        #endif
    >>I had a feeling it was "wrong"
    If you're not sure of what you're saying, then I'd suggest you be mindful of what you post.

    >>I knew it was obsolete, but it does work.
    Not my compiler.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    35
    yay! I can't get over how elegant this solution is. I love it. thank you all so much once more
    VIM + gcc + beer... does life get any better?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-10-2009, 02:51 PM
  2. Precompiled header question
    By donglee in forum C++ Programming
    Replies: 13
    Last Post: 01-22-2009, 01:23 AM
  3. changing the gcc standard include search path
    By RoshanX in forum Linux Programming
    Replies: 1
    Last Post: 01-01-2007, 09:23 AM
  4. to #include or not to #include
    By krygen in forum C++ Programming
    Replies: 9
    Last Post: 12-25-2004, 12:06 AM
  5. Header files
    By GaPe in forum C Programming
    Replies: 6
    Last Post: 02-01-2003, 03:39 PM

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