Results 1 to 6 of 6

Thread: [RESOLVED] redefinitions abound

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    530

    Resolved [RESOLVED] redefinitions abound

    Hi,
    I'm showing my inexperience here, but I believe I have made a mess of includes and I'm wondering if someone can give me some pointers. Here's the situation. I've written programs in C for a little while now, but none this complex, that require so many different files in the project.

    I have a file, called CustomTypes.h, that holds a number of structures and enums. I have a main file that calls a function in a display file. When main calls the display function, it passes a structure to it for display. Problem is, main has to include CustomTypes.h so it knows what the structure is. The display function also includes it so it knows what the structure is. That is, it is defined in the display function prototype, which is located in the display header file. Now, since both of these include the same CustomType.h, I am getting errors at compile time stating that the structures and enums inside CustomType.h are being redefined. How is this mess typically kept straight?
    Thanks.
    Last edited by rickford66; May 10th, 2011 at 12:43 PM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: redefinitions abound

    This is what header guards are for:

    Code:
    #ifndef __CUSTOM_TYPES_H__
    #define __CUSTOM_TYPES_H__
    
    // Your header definitions goes here.
    
    #endif
    This will make sure that your header definitions will only be defined once, even if the file is included at multiple locations.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    530

    Re: redefinitions abound

    That makes sense. I tried it, but I still get the errors. Do I have a syntax error here?

    Code:
    #ifndef SCREEN_STATE
    typedef enum SCREEN_STATE
    {
    	CREATE_POWER_UP_SCREEN,
    	SHOW_POWER_UP_SCREEN,
    	CREATE_SENSOR_CAPACITY_SCREEN,
    	SHOW_SENSOR_CAPACITY_SCREEN,
    	CREATE_MAIN_DISPLAY_SCREEN,
    	SHOW_MAIN_DISPLAY_SCREEN,
    	CREATE_MAIN_MENU_SCREEN,
    	SHOW_MAIN_MENU_SCREEN,
    	CREATE_MODE_SCREEN,
    	SHOW_MODE_SCREEN,
    };
    #endif

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: redefinitions abound

    There's no syntax error, but you've got it a bit wrong.
    The ifndef directive checks if a preprocessor directive is defined or not. You need to add a #define inside, the enum will not make a difference for the #ifndef directive.

    Also, this might just be a small subset of your code that you've simplified, but you should really wrap your entire header in one big header guard.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    530

    Re: redefinitions abound

    I think I got it now. Thanks.

    Code:
    #ifndef _CUSTOM_TYPES
       #define _CUSTOM_TYPES
       //header definitions go here
    #endif

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    530

    Re: redefinitions abound

    Which is exactly what you posted earlier. haha

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width