简体   繁体   中英

How To Run Google test from terminal on C code?

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++ ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM