I am trying to build a googletest unit test for a proof of concept as a new unit testing framework that we could possibly use. In googletest, there are two ways to write a unit test: with a main, or without a main. If you do not define a main, you can link in the gtest_main library, which includes a main() function for you, saving you some time. In my environment, we use Jam to build binaries. I have gotten the binary to compile with main() in my code & by using the libgtest library, but I am looking for how to build it in Jam without the main.
Base case (with a main() function):
I am able to build a binary with this:
Main MyUnitTestBinary : MyClass.cpp ;
LinkLibraries MyUnitTestBinary : libgtest ;
Library libgtest : $(GTEST_DIR)/src/gtest-all.cc ;
Broken case (without a main() function):
I am not able to build a binary with this. I see many errors when I attempt to link the objects:
Main sample1_unittest : sample1.cc sample1_unittest.cc ;
LinkLibraries sample1_unittest : gtest_main ;
Library gtest_main : $(GTEST_DIR)/src/gtest_main.cc ;
I get many Linker Errors relating to undefined reference to blah. The undefined reference seems to be coming from the testing::internal namespace, which is not part of my code.
Any thoughts on how I can attack this, or look for more clues on the problem?
I found the answer! I was incorrectly not adding libgtest and gtest_main to the binary. I thought that gtest_main also included the definitions for the framework, and you needed to link up one or the other. In fact, you always need to link up libgtest, and you need to link up gtest_main only if you do not want to change up the standard main() function.
So...the correct answer is to add:
LinkLibraries sample1_unittest : libgtest gtest_main ;
Library libgtest : $(GTEST_DIR)/src/gtest-all.cc ;