I want to be able to use Cmockery to mock C functions called from C++ code I'm testing. As a step towards that, I've renamed the Cmockery example run_tests.c to run_tests.cpp, and am attempting to compile and link it with cmockery.c:
g++ -m32 -DHAVE_CONFIG_H -DPIC -I ../cmockery-0.1.2 -I /usr/include/malloc -c run_tests.cpp -o obj/run_tests.o
gcc -m32 -DHAVE_CONFIG_H -DPIC -Wno-format -I ../cmockery-0.1.2 -I /usr/include/malloc -c ../cmockery-0.1.2/cmockery.c -o obj/cmockery.o
g++ -m32 -o run_tests obj/run_tests.o obj/cmockery.o
The first two command lines (to compile) are successful, but after the last I get:
Undefined symbols:
"_run_tests(UnitTest const*, unsigned long)", referenced from:
_main in run_tests.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
That undefined symbol is from line 29 of run_tests.cpp:
return run_tests(tests);
The run_tests() function is defined in cmockery.c.
After reading "Linking C++ code with 'gcc' (without g++)", I tried:
gcc -lstdc++ -m32 -o run_tests obj/run_tests.o obj/cmockery.o
But got the same result:
Undefined symbols:
"_run_tests(UnitTest const*, unsigned long)", referenced from:
_main in run_tests.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
How do I compile and link C++ code so it finds the symbols in C code?
I think that you can get thinkgs to link from C++ by adding the following around the contents of the cmockery.h file:
At or near the beginning:
#if defined(__cplusplus)
extern "C" {
#endif
At or near the end:
#if defined(__cplusplus)
}
#endif
That way, use of the header in C sources will ignore the extern "C"
part of the declaration, but when the header is include in C++ builds, the compiler will be properly told that the linkage for the declarations in that header use C semantics.
For a quick-n-dirty test or if you'd rather not modify the header, you can try:
extern "C" {
#include "cmockery.h"
}
but my preference would be to put the extern "C"
block in the header (and only around the stuff that's required - that might need a bit of analysis).