Including the most common libraries in Makefiles for unit test files:

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

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

and for other linker options I will put "-pthread". I know that somehow these libraries should be added to the makefile, but I cannot figure out how to do this. Initially, I thought they needed to be added to line 3, but all I'm trying is returning thousands of lines of error like:

 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 *~
+5
source share
2

:

g++ -o Test Test_A.o /usr/lib/libgtest.a /usr/lib/libgtest_main.a -lpthread
+6

. LDFLAGS - , Make , , .

  • libabc.a libabc.so, -labc.

  • , -L. , LD_LIBRARY_PATH , /etc/ld.so.conf.

-L/usr/lib ( ld.so.conf , /usr/lib ), , .

LDFLAGS := -lpthread -lgtest -lgtest_main -L/usr/lib

all: Test

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

Test: Test_A.o
     g++ -o Test Test_A.o $(LDFLAGS)

clean:
     rm -rf *o *~
+6

All Articles