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?