Question

I'm building an application that needs to compile on both Windows and Linux. The application is written in C, almost everything works except the MinGW compiler refuses this

typedef struct somestruct{
   ...snip...
   enum {NODE, REAL} type;
};

somestruct* something;
switch (something->type){
 case NODE:
   ...stuff...;
   break;
 case REAL:
   ...otherstuff...;
   break;
}

It says NODE and REAL are not defined, But if I supply a scope resolution

case somestruct::NODE

This compiles with MinGW 3.4.1, but fails to compile with gcc 4.1.2 on linux. Is this simply a compiler issue that needs to be resolved with preprocessors or is there some other explanation?

Was it helpful?

Solution

If you get rid of the nesting, it should work portably:

typedef enum somestruct_type {
    somestruct_type_NODE, somestruct_type_REAL
} somestruct_type;
typedef struct somestruct {
   ...snip...
   somestruct_type type;
} somestruct;

I have seen code very similar to this be ported to a large number of C and C++ compilers.

(I'm not saying this is the only way to do it; I'm just saying that this way works).

OTHER TIPS

The app is written in "C", yet the compiler demands that you do somestruct::NODE, which is valid "C++", but not valid "C".

Conclusion: you are compiling this code with MinGW in C++ mode, but with all the other compilers in C mode.

Probable cause: MinGW may not treat foo.C and foo.c the same, and your files are named with a capital C suffix (which implies C++ on UNIX).

Solution: add -xc flag to MinGW to force plain-C compiles.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top