Google's C++ Test Framework has two output libraries: one is gtest.lib and the other one is gtest_main.lib. According to Nik Reiman's answer on how to setup gtest with Visual Studio, we should link to gtest_main.lib but I'm linking to gtest.lib and the sample test cases that I have are running fine.
Google的C ++ Test Framework有两个输出库:一个是gtest.lib,另一个是gtest_main.lib。根据Nik Reiman关于如何使用Visual Studio设置gtest的答案,我们应该链接到gtest_main.lib但是我链接到gtest.lib并且我运行的示例测试用例运行正常。
What's the difference between the two libraries and does it matter which one I link to?
两个库之间有什么区别,我链接到哪一个是否重要?
3 个解决方案
#1
21
the only reasonable difference is that gtest_main.lib provides a default implementation of a test application entry point (i.e. main
function):
唯一合理的区别是gtest_main.lib提供了测试应用程序入口点的默认实现(即主函数):
Citation from Getting started with Google C++ Testing Framework:
来自Google C ++测试框架入门的引文:
"[...] maybe you think that writing all those main() functions is too much work? We agree with you completely and that's why Google Test provides a basic implementation of main(). If it fits your needs, then just link your test with gtest_main library and you are good to go."
“[...]也许您认为编写所有main()函数的工作量太大了?我们完全同意您的看法,这就是为什么Google Test提供main()的基本实现。如果它符合您的需求,那么只需链接使用gtest_main库进行测试,你很高兴。“
If you want to write your main function yourself - you should link with gtest.lib.
如果你想自己编写主要功能 - 你应该与gtest.lib链接。
#2
2
In fact, the various build methods available for googletest don't build the libraries consistently. At least this part is consistent though:
实际上,googletest可用的各种构建方法并不能始终如一地构建库。至少这部分是一致的:
gtest
The gtest library (variously called gtest.a
, gtest.so
, gtest.lib
or libgtest.a
, etc, depending on your platform and whether you are using the shared library) contains the object code for the gtest framework, including everything that tests need. Basically it implements everything you can use from gtest/gest.h
. It does not include a main()
method.
gtest库(不同地称为gtest.a,gtest.so,gtest.lib或libgtest.a等,取决于您的平台以及您是否使用共享库)包含gtest框架的目标代码,包括测试的所有内容需要。基本上它实现了你可以从gtest / gest.h中使用的所有东西。它不包含main()方法。
gtest_main
It includes a trivial main method that will launch the registered tests, something like this (as of 1.8):
它包括一个简单的main方法,它将启动已注册的测试,如下所示(从1.8开始):
GTEST_API_ int main(int argc, char **argv) {
printf("Running main() from gtest_main.cc\n");
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Now the inconsistent part is that gtest_main
sometimes also includes everything from gtest
, so that you only need to link against either gtest
(if you want to write your own main()
method) or gtest_main
(if you want the use the canned main method above). This is the case, for example, if you use the Makefile
build included in googletest/make
:
现在不一致的部分是gtest_main有时也包含来自gtest的所有东西,所以你只需要链接gtest(如果你想编写自己的main()方法)或gtest_main(如果你想使用上面的canned main方法) )。例如,如果您使用googletest / make中包含的Makefile构建,就是这种情况:
gtest.a : gtest-all.o
$(AR) $(ARFLAGS) $@ $^
gtest_main.a : gtest-all.o gtest_main.o
$(AR) $(ARFLAGS) $@ $^
Clearly, gtest_main.a
includes everything that gtest.a
does, plus the gtest-main.o
object which includes the main function.
显然,gtest_main.a包含gtest.a所做的一切,以及包含main函数的gtest-main.o对象。
With the CMake build, however, the situation is different, at least for some build artifacts. For example, for the main libraries we have:
但是,使用CMake构建时,情况会有所不同,至少对于某些构建工件而言。例如,对于我们的主库:
cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
target_link_libraries(gtest_main gtest)
Here, gtest_main
only contains the main function and nothing much else1. The target_link_libraries
line tells anything else using this CMake build that if you link gtest_main
you should also link gtest
, so in the rest of the file it is common to see things linked only against gtest_main
. Indeed, the documentation earlier in the CMakeLists.txt
file makes this explicit:
在这里,gtest_main只包含main函数而没有其他内容。 target_link_libraries行告诉使用此CMake构建的任何其他内容,如果链接gtest_main,您还应链接gtest,因此在文件的其余部分中,通常会看到仅针对gtest_main链接的内容。实际上,CMakeLists.txt文件中较早的文档使其明确:
# Defines the gtest & gtest_main libraries. User tests should link
# with one of them.
Note the "with one one them" part. What they really mean is that if you are building with this same CMake system you can do that, but at the actual link level you need both libtest.a
and libgtest_main.a
or else you won't pull in what you need to write a test.
注意“有一个人”的部分。他们真正的意思是,如果你使用相同的CMake系统构建你可以做到这一点,但在实际的链接级别你需要libtest.a和libgtest_main.a或者你不会拉你需要写的东西测试。
1 Indeed, with CMake libgtest.a ends up at 1,755,216 bytes, and libgtest_main.a is only a paltry 3,836 bytes. With the ../make/Makefile
build, those figures are 3,365,240 and 3,398,356 respectively. Evidently there are differences beyond the files included that blow up the size of the Makefile
version.
1实际上,使用CMake libgtest.a最终为1,755,216字节,而libgtest_main.a只有一个微不足道的3,836字节。使用../make/Makefile构建,这些数字分别为3,365,240和3,398,356。显然,除了包含的文件之外,还有一些差异会炸毁Makefile版本的大小。
#3
-3
You will need to link gtest.lib
to your project with the unit tests.
您需要使用单元测试将gtest.lib链接到您的项目。
#1
21
the only reasonable difference is that gtest_main.lib provides a default implementation of a test application entry point (i.e. main
function):
唯一合理的区别是gtest_main.lib提供了测试应用程序入口点的默认实现(即主函数):
Citation from Getting started with Google C++ Testing Framework:
来自Google C ++测试框架入门的引文:
"[...] maybe you think that writing all those main() functions is too much work? We agree with you completely and that's why Google Test provides a basic implementation of main(). If it fits your needs, then just link your test with gtest_main library and you are good to go."
“[...]也许您认为编写所有main()函数的工作量太大了?我们完全同意您的看法,这就是为什么Google Test提供main()的基本实现。如果它符合您的需求,那么只需链接使用gtest_main库进行测试,你很高兴。“
If you want to write your main function yourself - you should link with gtest.lib.
如果你想自己编写主要功能 - 你应该与gtest.lib链接。
#2
2
In fact, the various build methods available for googletest don't build the libraries consistently. At least this part is consistent though:
实际上,googletest可用的各种构建方法并不能始终如一地构建库。至少这部分是一致的:
gtest
The gtest library (variously called gtest.a
, gtest.so
, gtest.lib
or libgtest.a
, etc, depending on your platform and whether you are using the shared library) contains the object code for the gtest framework, including everything that tests need. Basically it implements everything you can use from gtest/gest.h
. It does not include a main()
method.
gtest库(不同地称为gtest.a,gtest.so,gtest.lib或libgtest.a等,取决于您的平台以及您是否使用共享库)包含gtest框架的目标代码,包括测试的所有内容需要。基本上它实现了你可以从gtest / gest.h中使用的所有东西。它不包含main()方法。
gtest_main
It includes a trivial main method that will launch the registered tests, something like this (as of 1.8):
它包括一个简单的main方法,它将启动已注册的测试,如下所示(从1.8开始):
GTEST_API_ int main(int argc, char **argv) {
printf("Running main() from gtest_main.cc\n");
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Now the inconsistent part is that gtest_main
sometimes also includes everything from gtest
, so that you only need to link against either gtest
(if you want to write your own main()
method) or gtest_main
(if you want the use the canned main method above). This is the case, for example, if you use the Makefile
build included in googletest/make
:
现在不一致的部分是gtest_main有时也包含来自gtest的所有东西,所以你只需要链接gtest(如果你想编写自己的main()方法)或gtest_main(如果你想使用上面的canned main方法) )。例如,如果您使用googletest / make中包含的Makefile构建,就是这种情况:
gtest.a : gtest-all.o
$(AR) $(ARFLAGS) $@ $^
gtest_main.a : gtest-all.o gtest_main.o
$(AR) $(ARFLAGS) $@ $^
Clearly, gtest_main.a
includes everything that gtest.a
does, plus the gtest-main.o
object which includes the main function.
显然,gtest_main.a包含gtest.a所做的一切,以及包含main函数的gtest-main.o对象。
With the CMake build, however, the situation is different, at least for some build artifacts. For example, for the main libraries we have:
但是,使用CMake构建时,情况会有所不同,至少对于某些构建工件而言。例如,对于我们的主库:
cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
target_link_libraries(gtest_main gtest)
Here, gtest_main
only contains the main function and nothing much else1. The target_link_libraries
line tells anything else using this CMake build that if you link gtest_main
you should also link gtest
, so in the rest of the file it is common to see things linked only against gtest_main
. Indeed, the documentation earlier in the CMakeLists.txt
file makes this explicit:
在这里,gtest_main只包含main函数而没有其他内容。 target_link_libraries行告诉使用此CMake构建的任何其他内容,如果链接gtest_main,您还应链接gtest,因此在文件的其余部分中,通常会看到仅针对gtest_main链接的内容。实际上,CMakeLists.txt文件中较早的文档使其明确:
# Defines the gtest & gtest_main libraries. User tests should link
# with one of them.
Note the "with one one them" part. What they really mean is that if you are building with this same CMake system you can do that, but at the actual link level you need both libtest.a
and libgtest_main.a
or else you won't pull in what you need to write a test.
注意“有一个人”的部分。他们真正的意思是,如果你使用相同的CMake系统构建你可以做到这一点,但在实际的链接级别你需要libtest.a和libgtest_main.a或者你不会拉你需要写的东西测试。
1 Indeed, with CMake libgtest.a ends up at 1,755,216 bytes, and libgtest_main.a is only a paltry 3,836 bytes. With the ../make/Makefile
build, those figures are 3,365,240 and 3,398,356 respectively. Evidently there are differences beyond the files included that blow up the size of the Makefile
version.
1实际上,使用CMake libgtest.a最终为1,755,216字节,而libgtest_main.a只有一个微不足道的3,836字节。使用../make/Makefile构建,这些数字分别为3,365,240和3,398,356。显然,除了包含的文件之外,还有一些差异会炸毁Makefile版本的大小。
#3
-3
You will need to link gtest.lib
to your project with the unit tests.
您需要使用单元测试将gtest.lib链接到您的项目。