# Enumerations in C Library Header Problem

This topic is 3542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm writing a library in C and have run into a problem with enumerations. Because I define enumerations in library headers, they are defined in both the resulting static library and a simple testing program that includes the library header. At least, this is what I suspect is causing the problem. I get multiple link errors claiming there are multiple definitions of certain enumerators. Here's an example. lib.h:
...
typedef enum foo {
bar = 0x00
} foo;
extern void baz(void);
...


lib.c:
...
#include "lib.h"

void baz(void) {
...
int a = bar; // Use the enumerator.
...
}
...


prog.c:
...
#include "lib.h"

int main(void) {
baz(); // Function from the library.
return 0;
}
...


This looks something like what's going on with my library/program code. I get errors about 'bar' having multiple definitions. I'm pretty sure this is because it is defined in lib.h, which is included in both the library and program source files and results in 'bar' being defined in the object files of the library and program. Once they link... bam! Two definitions. Is there a way to avoid this problem? I'd really like to stay away from macro-based constants! I'm curious as to why I haven't run into similar problems in C++ before, but I could just be way off the mark as to what's actually going on. Thanks for the help!

##### Share on other sites
Arg, ignore me. It turns out that the errors weren't referring to enumerators at all! I left some silly constant definitions lying around that caused the problem. I was using them as some sort of alias for certain enumerators. Something like this.

lib.h:
...typedef enum foo {    bar = 0x00} foo;const int alt = bar;...

It was 'alt' that was giving me the trouble. Whoops. [lol]

I'm about to remove those pesky constants and hopefully this problem will go away.

EDIT: Yep, all fixed.

1. 1
2. 2
3. 3
Rutin
24
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002228
×