Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

enums in C header files

Posted on 2010-09-24
6
Medium Priority
?
416 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
ID: 33755548
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 2000 total points
ID: 33755573
>> (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
ID: 33755598
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:riceman0
ID: 33755652

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
ID: 33755696
>> 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
ID: 33755713
>> 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

569 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