I'm having a compilation error in my C project. I have a header file that contains this enumeration:
typedef enum {
RAD_ALLOWED= 0,
RAD_STOPPED ,
RAD_OFF
} Values_E;
and in another header file with this enum:
typedef enum {
RAD_ALLOWED= 0,
RAD_STOPPED ,
RAD_OFF
} Values_X;
when I include both header files in the same c file I'm having errors similar to:
214: error: previous definition of 'RAD_STOPPED ' was here
129: error: redeclaration of enumerator 'RAD_STOPPED '
Yes, the content of both enumeration is the same but names are different so i don't understand why do i have this problem. Note that the both header files that contains those enumeration are automatically generated so I can't change their content.
The problem is not a clash of the enumeration names, it is a clash of the names of the enumeration constants they declare. The names of enumeration constants live in the namespace of ordinary identifiers, so they must be distinct across all in-scope enumeration definitions. And that makes sense in light of the fact that you use them without reference to the enumeration that defines them.
If you use only the enum constants and not the enum types, then you can just choose one of the enum definitions to include, and omit the other. For example,
#include "values_e.h"but ignorevalues_x.h. The two enum definitions shown will produce enum constants corresponding to the same values, so if only the values of the constants matter then it doesn't matter which enum you get them from. This may be easier said than done, however, if any of the other headers you are using themselves include the enum definitions indirectly.Or possibly you can split your code so that no one source file needs to reference both enum types.
Otherwise, your next best bet would be to change your code generator so that it doesn't yield name clashes.
Or if you can't do that, then you could possibly apply some macro grease to effect a local renaming, like this:
Within that source file, you then use the
E_*andX_*versions of those identifiers instead of the unprefixed ones. There are potential gotchas with that, but it's worth a shot if you don't have a better alternative.