Thread: Avoiding multiple includes

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Avoiding multiple includes

    Hi all

    I met a 'little' problem concerning multiple includes of the same header file:
    Let's say I have the four files
    - user.c
    - user.h
    - capture.c
    - capture.h

    The file 'user.h' holds the struct definition:
    Code:
    struct user
    {
        char *name;
        int *ports;
        AES_KEY dkey;        //Encryption key of the user
        AES_KEY ekey;        //Decryption key of the user
        struct user *right;
        struct user *left;
    };
    The file 'capture.h' holds the struct definition:
    Code:
    struct knock_request
    {
        char *username;
        char *s_ip;
        time_t timestamp;
        AES_KEY dkey;        //Key to decrypt the data
        char *s_address;
        char *knock_data;
    };
    Now as I use 'AES_KEY' within the two structs, I have to include <openssl/aes.h> in the two header files, otherwise I get warnings when compiling?...So far that would be OK, there are no multiple includes of the same file till here.

    But let's assume that the file "capture.c" needs both "user.h" and "capture.h" included? I would have the file <openssl/aes.h> included two times. How should I avoid that? Is there some 'standard' way? Like using a void pointer to point to the actual encryption/decryption keys?

    Regards
    Rafael

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    The standard "Include Guard" method is like this:

    FileA.h:
    Code:
    #ifndef FILE_A_H_INCLUDED
    #define FILE_A_H_INCLUDED
    
    ...
    
    #endif /* FILE_A_H_INCLUDED */
    FileB.h:
    Code:
    #ifndef FILE_B_H_INCLUDED
    #define FILE_B_H_INCLUDED
    
    ...
    
    #endif /* FILE_B_H_INCLUDED */

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    22
    Sure thanks, I know that.
    But do all header files make use of that guard? If not it wouldn't be of use here.
    I just looked in the <openssl/aes.h> header file and saw that they're using the guard, didn't know that. But what if they didn't? What would I do then? As I don't want to rely on "everyone does it"...

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well everyone usually does do this. It's a convention.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    If someone was writting a library or something that was made to be redistributed, then it'd be stupid for them not to.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    22
    Ok then, that sounds good...
    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #include's in multiple modules
    By samus250 in forum C Programming
    Replies: 8
    Last Post: 03-24-2008, 02:12 PM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  3. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  4. deal with multiple includes
    By rockdj in forum C++ Programming
    Replies: 4
    Last Post: 07-29-2004, 09:07 AM
  5. multiple header #includes
    By bennyandthejets in forum Windows Programming
    Replies: 2
    Last Post: 12-30-2002, 10:04 PM