0
votes

I've made a simple main to check if gtest was correctly linked and got "undefined reference" errors. The first line in the main function throws an undefined reference to `testing::InitGoogleTest(int*, char**)'.

#include <gtest/gtest.h>

int main(int argc, char* argv[])
{
     testing::InitGoogleTest(&argc, argv);
     return RUN_ALL_TESTS();
}

I have successfully built a gtest Code::Block project with the help of cmake and without the pthread library. Then, I compiled everything with MinGW. I took the libgtest.a libgmock.a and linked them in my Code::Block project with no problems. I also specified the include directories and the binary directories. Is it possible that I need to include gtest_main.a too?

The build log (I added "..." to hide long chains of folders):

"obj\Debug gtest\MainTest.o" -lsfml-graphics-s-d -lsfml-window-s-d -lsfml-network-s-d -lsfml-audio-s-d -lsfml-system-s-d -lglew -lgtest -lgmock -lws2_32 -lopenal32 -lsndfile -lgdi32 -lwinmm -lfreetype -ljpeg -lglu32 -lopengl32 obj\Debug gtest\MainTest.o: In function main': D:/.../Templates/SFML 2_2 BasicScene/MainTest.cpp:5: undefined reference totesting::InitGoogleTest(int*, char**)' obj\Debug gtest\MainTest.o: In function Z13RUN_ALL_TESTSv': D:/.../googletest/googletest/include/gtest/gtest.h:2237: undefined reference totesting::UnitTest::GetInstance()' D:/.../googletest/googletest/include/gtest/gtest.h:2237: undefined reference to `testing::UnitTest::Run()'

1
To get a linkage failure explained it is essential that you provide the compiler commandline that executes the failing linkage, as well as the error messages. Post this information in the question.Mike Kinghan

1 Answers

1
votes

I finally found it! It was tricky: as I am using Windows 7 and installed multiple compilers (3 versions of MinGW and 2 of TDM), my PATH got all mixed up. When I was using cmake-gui 3.3, I was using the default compiler, which was MinGW 4.7 when I installed Code::Blocks. After installing all the compilers mentionned above, the default compiler path changed for a higher version of MinGW. Linking with a library that was compiled with a newer version of MinGW didn't cause the library to not be found, but instead caused linking errors.

In conclusion, I was using two differents versions of the same compiler.