Trying to link gtest libraries to a project, but the incorrect syntax is generated in the link.txt
I have a project that I am converting to CMake 3.13 on Ubuntu 18.04, and the generated link.txt for the test executable is not correct. I searched online for the solution and have not found one. I have replaced the link.txt with the correct linking syntax, rebuild, and everything links fine. If I reload the CMake file in CLion 2018.3, it reverts back and I get the following error:
CMakeFiles/Test_d.dir/TestMain.cpp.o: In function `DefaultModelTest_CanInstantiate_Test::TestBody()':
/project/test/DefaultModelTest.h:42: undefined reference to `testing::Message::Message()'
/project/test/DefaultModelTest.h:42: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/project/test/DefaultModelTest.h:42: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/project/test/DefaultModelTest.h:42: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/project/test/DefaultModelTest.h:42: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
...
The cmake syntax I have tried so far in my CMakeLists.txt:
- target_link_libraries(TestMain PUBLIC MyLib gtest gtest_main gmock gmock_main)
- SET(GCC_COVERAGE_LINK_FLAGS "${GCC_COVERAGE_LINK_FLAGS} -L/project/deps/gtest/libs -lgtest -lgtest_main -lgmock -lgmock_main")
The CMake generates a link.txt with:
/usr/bin/c++ -pthread -g CMakeFiles/Test.dir/TestMain.cpp.o -o TestMain -L/project/deps/gtest/libs/libgtest.a -L/project/deps/gtest/libs/libgtest_main.a -L/project/deps/gtest/libs/libgmock.a -L/project/deps/gtest/libs/libgmock_main.a -Wl,-rpath,/project/deps/gtest/libs/libgtest.a:/project/deps/gtest/libs/libgtest_main.a:/project/deps/gtest/libs/libgmock.a:/project/deps/gtest/libs/libgmock_main.a /project/libs/libMyLib.a -lgtest -lgtest_main -lgmock -lgmock_main
instead of:
/usr/bin/c++ -pthread -g CMakeFiles/Test_d.dir/TestMain.cpp.o -o TestMain -L/deps/gtest/libs -lgtest -lgtest_main -lgmock -lgmock_main -L/project/libs/ -lMyLib
I am still new to CMake, so I do not know if I am using the syntax improperly or if this is some kind of bug.
Edited by M Davis