Solved

enums in C header files

Posted on 2010-09-24
6
397 Views
Last Modified: 2012-05-10
I try to adhere to the following rules with respect to C headers and includes

0) each .h/.c file set is a C "unit"
1) the header file is the "public interface" of the unit
2) it includes prototypes of all functions to be called from outside
3) it does not define any data (e.g. int x;)
3a) this dovetails with the OO principle: for me all data is owned by a unit, no data is shared, if you want to access data in another unit write get/set functions
4) due to (3) a header file can be included more than one time in an application, from any other unit that needs to access it
5) each unit only #includes the other files whose functions it needs to call

This has been a great approach, worked well, minimizes and provides clear indication of the dependencies, kept me out of "include hell", trying to resolve circular dependencies (since it is no problem two units #include-ing each other's headers) or those dumb-seeming long #include lists that big (maybe sloppy?) C applications seem to use.

This has been such a good approach that I wonder if I'm finally doing it as intended, that (1) through (5) is exactly what the C designers had in mind.

But a problem I'm encountering is with enums.  Whereas I can #include a header file with only function declarations multiple times, if it has an enum definition I get a redefinition error.  But I would really like the enum to be part of the public interface; a get_mode function could return a value of one of several mode enumerations, for example.

If I have stumbled upon the "right" way to do this, then is there a way to use enums in the public interface consistent with my rules (1) - (5).

(I know I could use #define "guards" at the beginning and end of the header files to prevent them from being re-included, but I'd prefer not to if I can help it.)

Thanks for any thoughts.
0
Comment
Question by:riceman0
  • 5
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
You can put the typedef for the enum in the header file, and the enum variable definition in the .c file.
/* in the .h : */



typedef enum {

    MET_ONE,

    MET_TWO,

    MET_THREE

} MyEnumType;





/* in the .c : */



MyEnumType instance;

Open in new window

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
>> (I know I could use #define "guards" at the beginning and end of the header files to prevent them from being re-included, but I'd prefer not to if I can help it.)

You should always add include guards. It's what allows you to correctly include the same header file more than once.
#ifndef MY_HEADER_H

#define MY_HEADER_H



/* contents of the header file */



#endif /* MY_HEADER_H */

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
Note that typedefs can be part of the public interface of a module (and they commonly are). Function declarations, and extern variable declarations are also commonly part of the public interface of a module.

So there's nothing wrong with adding them in the header file (assuming that you want to be able to use them outside of the module) - in fact it's what you'll have to do to write useful code.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:riceman0
Comment Utility

Actually I think I was doing the equivalent, right?

enum e_positioner_msg
{
      move_XYstage_move_complete,
      move_Zstage_move_complete,
      move_Zlens_move_complete
};

Neither this nor your typedef syntax allows itself to be redefined (i.e., included from all units that need it) either.

Although I can anticipate your answer: prevent redefinition using include guards.

I thought I had avoided the need for include guards by going to a pure function-based interface (and it felt right).  But if I want to enrich the interface with enums, seems I have to use include guards.

0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> Actually I think I was doing the equivalent, right?

That's equivalent (except that a typedef makes it more convenient to use).


>> Although I can anticipate your answer: prevent redefinition using include guards.

Exactly ;)


>> I thought I had avoided the need for include guards

You can declare the same function multiple times within the same compilation unit. The compiler has no problem with that.

However, there is no reason for trying to avoid include guards. It is necessary in many cases, and makes compilation faster in pretty much all cases. Just use them in every header file - no questions asked :)
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> no questions asked :)

Obviously, by that, I didn't mean that you couldn't ask questions ;)
Just that, once you are convinced that include guards are useful, use them in every header file without thinking about it.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now