Thread: Header Files Typedef Redefintion

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    45

    Header Files Typedef Redefintion

    Hello,

    I have 3 c files and 2 header files:
    main.c, lifecycle.c, table.c, lifecycle.h and table.h
    I worked on the table in a different problem with a different main and it works fine.

    The issue I am having is that in both lifecycle.h and table.h I have
    Code:
    typedef enum BOOL { false, true } Boolean;
    So I get errors like: "error: redefinition of enumerator 'false'" or "note: previous definition is here"

    The way I have linked this is lifecycle.c include:
    Code:
    #include "lifecycle.h"
    #include "table.h"
    table.c includes only it's corresponding header file table.h.
    main.c includes only lifecycle.h

    I get that I should only have this typdef in 1 place because this duplication is causing error, but I'm not sure the best place to put this.
    I am not used to using this many files.

    So to clarify I want this typedef to work with table.c and lifecycle.c and main.c (all my c files).
    I tried moving this typedef but the 2 header files have Boolean returns so the headers must also have access to this typedef.

    Thanks,

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Do your header files have the proper inclusion guards?

    Please post the contents of the headers, or better yet a small complete program that illustrates the problem.

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    45
    By guards do you mean this?:
    Code:
    #ifndef _TABLE_H
    #define _TABLE_H
    Code:
    #ifndef _LIFECYCLE_H
    #define _LIFECYCLE_H
    At the end of each header file is the corresponding #endif.
    The functions in both header files have returns of Boolean as mentioned in the original post.

    Quote Originally Posted by jimblumberg View Post
    Do your header files have the proper inclusion guards?

    Please post the contents of the headers, or better yet a small complete program that illustrates the problem.

    Jim

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    If your compiler defaults to the C99 C Standard (or higher) or you are using a compiler flag to force C99 or higher, then just:

    Code:
    #include <stdbool.h>
    and use "bool", "true" and "false" as defined in this header file and the C99+ Standards.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by seal308 View Post
    By guards do you mean this?:
    Code:
    #ifndef _TABLE_H
    #define _TABLE_H
    Yes, those are include guards.

    You can move your typedef into yet another include file and include it in all your other files. That way the include guards on the new include file will stop it from being included multiple times.
    Code:
    #ifndef TYPES_H_
    #define TYPES_H_
    typedef enum BOOL { false, true } Boolean;
    #endif
    If you're allowed to use c99 (and why not?) then you can simply include stdbool.h in all your files and use that.
    Code:
    #include <stdbool.h>
    
    bool b = true;
    Last edited by algorism; 04-05-2016 at 04:40 PM.

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    45
    Actually I forgot to mention I'm supposed to use the header file given for lifecycle.h and not change it.
    In that header file I have
    Code:
    #ifndef _LIFECYCLE_H
    #define _LIFECYCLE_H
    
    
    typedef enum { false, true } Boolean;
    So I can't do this in lifecycle.h:
    Code:
    #ifndef _LIFECYCLE_H
    #define _LIFECYCLE_H
    #include "types.h"
    I can edit anything else in the other c and h files though.
    Last edited by seal308; 04-05-2016 at 04:03 PM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Then instead of doing the typedef in several include files only use the definition in the lifecycle.h file. Include that file in any file that requires that typedef.

    Also note that _LIFECYCLE_H is a bad practice because any identifier starting with an underscore and an upper case letter or another underscore are reserved.

    Jim

  8. #8
    Registered User
    Join Date
    Jan 2016
    Posts
    45
    Ok thank you.
    That seems to have worked.
    I just had the table.c and table.h file include the lifecycle.h file.
    eg
    Code:
    #ifndef _TABLE_H
    #define _TABLE_H
    #include "lifecycle.h"
    ...
    #endif
    I'll also made note of what you said about the underscore thing. Thank you.

    *edit: realized I didn't need to include it in both table.c and table.h only table.h because table.c includes the table.h.

    Quote Originally Posted by jimblumberg View Post
    Then instead of doing the typedef in several include files only use the definition in the lifecycle.h file. Include that file in any file that requires that typedef.

    Also note that _LIFECYCLE_H is a bad practice because any identifier starting with an underscore and an upper case letter or another underscore are reserved.

    Jim
    Last edited by seal308; 04-05-2016 at 04:17 PM.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    *edit: realized I didn't need to include it in both table.c and table.h only table.h because table.c includes the table.h.
    Actually IMO you should include it in both table.c and table.h if both of these files require the typedef. Never rely on some other file including a required header.


    Jim

  10. #10
    Registered User
    Join Date
    Jan 2016
    Posts
    45
    ok, will do, thanks for the advice.

  11. #11
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Out of curiosity, Which compiler & version are you using, and what C Standard does your compiler default to, and are you overriding that default with a lower or higher level?

    Possible levels are:

    C89/90
    C99
    C11

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using typedef'ed types in h-files
    By jsaetrum in forum C Programming
    Replies: 3
    Last Post: 12-26-2011, 09:52 AM
  2. Typedef in a header?
    By dnguyen1022 in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2009, 09:51 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  5. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM