简体   繁体   English

gtest.lib和gtest_main.lib有什么区别?

[英]What's the difference between gtest.lib and gtest_main.lib?

Google's C++ Test Framework has two output libraries: one is gtest.lib and the other one is gtest_main.lib. Google的C ++ Test Framework有两个输出库:一个是gtest.lib,另一个是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. 根据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? 两个库之间的区别是什么,我链接到哪一个是否重要?

the only reasonable difference is that gtest_main.lib provides a default implementation of a test application entry point (ie main function): 唯一合理的区别是gtest_main.lib提供了测试应用程序入口点的默认实现(即main函数):

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链接。

In fact, the various build methods available for googletest don't build the libraries consistently. 事实上,googletest可用的各种构建方法并不能始终如一地构建库。 At least this part is consistent though: 至少这部分是一致的:

gtest 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. gtest库(不同地称为gtest.agtest.sogtest.liblibgtest.a等,取决于您的平台以及您是否使用共享库)包含gtest框架的目标代码,包括测试的所有内容需要。 Basically it implements everything you can use from gtest/gest.h . 基本上它实现了你可以从gtest/gest.h使用的所有东西。 It does not include a main() method. 包含main()方法。

gtest_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). 现在矛盾的是, gtest_main有时包括一切从gtest ,这样你需要对任何链接gtest (如果你想编写自己main()方法) gtest_main如果你想使用罐装主要方法以上( )。 This is the case, for example, if you use the Makefile build included in googletest/make : 例如,如果您使用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. 但是,使用CMake构建时,情况会有所不同,至少对于某些构建工件而言。 For example, for the main libraries we have : 例如,对于我们的主库:

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 else 1 . 这里, gtest_main 包含main函数,没有其他1 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 . target_link_libraries行告诉使用此CMake构建的任何其他内容,如果链接gtest_main您还应链接gtest ,因此在文件的其余部分中,通常会看到仅针对gtest_main链接的gtest_main Indeed, the documentation earlier in the CMakeLists.txt file makes this explicit: 实际上, 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.alibgtest_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. 1实际上,使用CMake libgtest.a最终为1,755,216字节,而libgtest_main.a只有一个微不足道的3,836字节。 With the ../make/Makefile build, those figures are 3,365,240 and 3,398,356 respectively. 使用../make/Makefile构建,这些数字分别为3,365,240和3,398,356。 Evidently there are differences beyond the files included that blow up the size of the Makefile version. 显然,除了包含的文件之外,还有一些差异会炸掉Makefile版本的大小。

您需要使用单元测试将gtest.lib链接到您的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM