Reputation: 351
I am testing C code using googleTest. My test.cpp file look like that
#include <gtest/gtest.h>
extern "C" {
#include "list.h"
#include "list.c"
}
TEST(ListTest, singleInsertion) {
// some tests
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
However trying to run the test from the terminal using
g++ test.cpp -lgtest
gives Errors and warning as if the code being tested is C++ not C.
Error and warning Examples :
error: invalid conversion for mallocs
and
warning: ISO C++ forbids converting a string constant to ‘char*'
how can I declare that my tested files are C not C++ ?
Upvotes: 3
Views: 2779
Reputation: 123538
However trying to run the test from the terminal using
g++ test.cpp -lgtest
gives Errors and warning as if the code being tested is C++ not C.
That's because you are compiling it as C++ by using the g++
compiler. Use gcc
to compile as C.
Unfortunately, this code won't compile as C - it'll choke on the google::InitGoogleTest()
call because C doesn't recognize the ::
scoping operator. I'm not familiar with this testing framework, but at first glance it looks like it's meant to be used with C++, not C.
The way to fix this is to remove the #include "list.c"
directive
extern "C" {
#include "list.h"
}
and compile it separately as C:
gcc -c list.c
then compile your tester:
g++ -c test.cpp
and then link the object files with the library:
g++ -o test test.o list.o -lgtest
Upvotes: 5