Leif Andersen
Leif Andersen

Reputation: 22332

C header file won't compile with C, but will with C++

I have the following chunk of a header file BKE_mesh.h:

/* Connectivity data */
typedef struct IndexNode {
    struct IndexNode *next, *prev;
    int index;
} IndexNode;
void create_vert_face_map(ListBase **map, IndexNode **mem, const struct MFace *mface,
          const int totvert, const int totface);
void create_vert_edge_map(ListBase **map, IndexNode **mem, const struct MEdge *medge,
          const int totvert, const int totedge);

Note that the header file was prepared for the possibility of being used in a C++ file, as it had:

#ifdef __cplusplus
extern "C" {
#endif

at the top of the file, and the needed finish at the bottom. But the class implementing it was written in C.

Next, whenever I try to #include the header file, I get an odd error. If the file has a .cpp extension, it compiles just fine, no complaints whatsoever. However, if I do:

#include "BKE_mesh.h"

inside of a file with a .c extension, I get the following errors:

expected ')' before '*' token

for the two last functions, in specific, the variable:

ListBase **map

in both classes. (Note that earlier in the header file, it declared, but not defined ListBase).

So, my question is: why is this valid C++ code, but not C code?

Thank you.

Upvotes: 0

Views: 382

Answers (2)

Yotam
Yotam

Reputation: 888

Try running just the pre-processor for each case. Comparing the result may show different header files. If so, it may hint on the "C"-problem.

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 361546

In C++ you can refer to struct names directly but in C you need to prepend the keyword struct.

void create_vert_face_map(struct ListBase **map, ... );

You could get around this by adding a typedef. Then you wouldn't have to modify the function declaration.

typedef struct ListBase ListBase;

Upvotes: 6

Related Questions