Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including gtest libraries in Makefiles for unit test files:

I am currently learning how to make and use MakeFiles for programs in C++. I have been able to create and run Makefiles for normal .cpp classes but I am having a problem with test classes. For testing I am using Gtest in Code::Blocks, and in my Test_A.cpp file in the "Linker Settings" I add:

 /usr/lib/libgtest.a
 /usr/lib/libgtest_main.a

and for the other linker options I put "-pthread". I know that in some way those libraries need to be added in the makefile, but I cannot figure how. I originally thought they need to be added in line 3, but everything I try returns thousands of lines of error of type:

 undefined reference to `testing::Test::TearDown()
 undefined reference to `testing::Test::~Test()  etc....

My makefile:

1. all: Test

2. Test_A.o: Test_A B.h
3.      g++ -c Test_A.cpp -o Test_A.o

4. Test: Test_A.o
5.      g++ -o Test Test_A.o

6. clean:
7.      rm -rf *o *~
like image 947
FranXh Avatar asked Mar 16 '13 04:03

FranXh


1 Answers

Libraries are added on the link line:

g++ -o Test Test_A.o /usr/lib/libgtest.a /usr/lib/libgtest_main.a -lpthread
like image 152
Vaughn Cato Avatar answered Oct 18 '22 04:10

Vaughn Cato