Jump to content
  • Advertisement
Sign in to follow this  
GenuineXP

Enumerations in C Library Header Problem

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!