Sign in to follow this  
GenuineXP

Enumerations in C Library Header Problem

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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this