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

发布于 2024-11-16 18:35:26 字数 468 浏览 3 评论 0 原文

Google 的 C++ 测试框架有两个输出库:一个是 gtest.lib,另一个是 gtest_main.lib。根据 Nik Reiman 的回答 href="https://stackoverflow.com/questions/531941/how-to-setup-google-c-testing-framework-gtest-on-visual-studio-2005">如何使用 Visual Studio 设置 gtest,我们应该链接到 gtest_main.lib,但我链接到 gtest.lib 并且我拥有的示例测试用例运行良好。

这两个库之间有什么区别?链接到哪一个有什么关系吗?

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.

What's the difference between the two libraries and does it matter which one I link to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咆哮 2024-11-23 18:35:26

唯一合理的区别是 gtest_main.lib 提供了测试应用程序入口点的默认实现(即 main 函数):

引用自 Google C++ 测试框架入门

“[...]也许你认为写作
所有这些 main() 函数太多了
工作?我们完全同意你的观点并且
这就是为什么 Google Test 提供了
main() 的基本实现。如果它
满足您的需求,然后只需链接您的
使用 gtest_main 库进行测试
一切顺利。”

如果您想自己编写主要函数 - 您应该链接到 gtest.lib

the only reasonable difference is that gtest_main.lib provides a default implementation of a test application entry point (i.e. main function):

Citation from Getting started with Google C++ Testing Framework:

"[...] 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."

If you want to write your main function yourself - you should link with gtest.lib.

梦年海沫深 2024-11-23 18:35:26

事实上,可用于 googletest 的各种构建方法并不能一致地构建库。至少这部分是一致的:

gtest

gtest 库(分别称为 gtest.agtest.sogtest.liblibgtest.a 等,具体取决于您的平台以及您是否使用共享库)包含 gtest 框架的目标代码,包括测试所需的所有内容。基本上它实现了您可以从 gtest/gest.h 中使用的所有内容。它包含main()方法。

gtest_main

它包含一个简单的 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();
}

现在不一致的部分是 gtest_main 有时包含 < code>gtest,这样您只需需要链接到任一 gtest(如果您想编写自己的 main() 方法) gtest_main(如果您想使用上面的固定 main 方法)。例如,如果您使用 Makefile 构建,就是这种情况rel="noreferrer">googletest/make

gtest.a : gtest-all.o
    $(AR) $(ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
    $(AR) $(ARFLAGS) $@ $^

显然,gtest_main.a 包含 gtest.a 所做的一切,加上 gtest-main.o 对象,其中包含 main 函数。

然而,对于 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)

这里, gtest_main 包含 main 函数,没有其他内容1target_link_libraries 行告诉任何其他使用此 CMake 构建的内容,如果您链​​接 gtest_main,则还应该链接 gtest,因此在文件的其余部分中,通常会看到仅与 gtest_main 链接的内容。事实上,CMakeLists.txt 文件中前面的文档明确说明了这一点:

# Defines the gtest & gtest_main libraries.  User tests should link
# with one of them.

请注意“与其中一个”部分。他们真正的意思是,如果您使用相同的 CMake 系统进行构建,您可以做到这一点,但在实际的链接级别,您需要 libtest.alibgtest_main.a否则你将无法提取编写测试所需的内容。


1 事实上,使用 CMake libgtest.a 最终大小为 1,755,216 字节,而 libgtest_main.a 只有区区 3,836 字节。使用 ../make/Makefile 构建时,这些数字分别为 3,365,240 和 3,398,356。显然,除了所包含的文件之外,还存在一些差异,这些差异会导致 Makefile 版本的大小增大。

In fact, the various build methods available for googletest don't build the libraries consistently. At least this part is consistent though:

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_main

It includes a trivial main method that will launch the registered tests, something like this (as of 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.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.

With the CMake build, however, the situation is different, at least for some build artifacts. 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 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:

# 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.


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.

缱绻入梦 2024-11-23 18:35:26

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

You will need to link gtest.lib to your project with the unit tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文