I finally went ahead and tested it. Here are the test programs:
test_cpp.cpp:
#include "test.h"
class C {
int a;
public:
C() : a(42) {}
int getA() { return a; }
void setA(int v) { a=v; }
};
int get7(bool b) { return b ? 7 : 3; }
C c;
C* getC() { return &c; }
int getA(C* t) { return t->getA(); }
void setA(C* t, int v) { return t->setA(v); }
test_c.c:
#include <stdio.h>
#include "test.h"
int main()
{
C* c = getC();
printf("%d\n", getA(c));
setA(c, 10);
printf("%d\n", getA(c));
printf("%d\n%d\n%d\n%d\n", get7(0), get7(1), get7(2), get7(-1));
return 0;
}
test.h:
#ifdef __cplusplus
extern "C" {
#else
#define bool _Bool
#endif
struct C;
typedef struct C C;
int get7(bool b);
C* getC();
int getA(C* t);
void setA(C* t, int v);
#ifdef __cplusplus
}
#endif
Compiling with mingw64 gcc-4.9.0 on windows
gcc -c test_c.c
g++ test_cpp.cpp test_c.o
it compiled and ran correctly. Points of interest:
g++
had no problem at all withC
being declared as astruct
and later defined as aclass
- c++
bool
andc
_Bool worked interchangeably
I tried this with MS compiler (VS2012) and it worked the same except for one small detail that I can't explain: In the header file, I had to change
int get7(bool b);
to
int get7(bool);
to get it to compile. If anyone can explain this I would be happy to understand it.