如何开始使用 GTest 和 CMake [英] How to start working with GTest and CMake

查看:21
本文介绍了如何开始使用 GTest 和 CMake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近热衷于使用 CMake 来编译我的 C++ 项目,现在想开始为我的代码编写一些单元测试.我已决定使用 Google Test 实用程序来帮助解决此问题,但需要一些入门帮助.

I have recently been sold on using CMake for compiling my C++ projects, and would now like to start writing some unit tests for my code. I have decided to use the Google Test utility to help with this, but require some help in getting started.

我整天都在阅读各种指南和示例,包括 入门IBM 介绍 以及关于 SO 的一些问题(此处此处) 以及我已经忘记的其他来源.我意识到那里有很多东西,但不知何故我仍然遇到困难.

All day I have been reading various guides and examples include the Primer, an introduction at IBM and some questions on SO (here and here) as well as other sources I've lost track of. I realise there's plenty out there but somehow I am still having difficulties.

我目前正在尝试实施最基本的测试,以确认我已经正确编译/安装了 gtest 并且它不起作用.唯一的源文件 (testgtest.cpp) 几乎完全来自 this 之前的答案:

I'm currently trying to implement the most basic test, to confirm I've compiled/installed gtest right and it's not working. The only source file (testgtest.cpp) is taken almost exactly from this previous answer:

#include <iostream>

#include "gtest/gtest.h"

TEST(sample_test_case, sample_test)
{
    EXPECT_EQ(1, 1);
}

和我关联的 CMakeLists.txt 如下:

and my associated CMakeLists.txt is as follows:

cmake_minimum_required(VERSION 2.6)
project(basic_test)

# Setup testing
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})

# Add test cpp file
add_executable(runUnitTests
    testgtest.cpp
)

# Link test executable against gtest & gtest_main
target_link_libraries(runUnitTests ${GTEST_LIBRARY_DEBUG} ${GTEST_MAIN_LIBRARY_DEBUG})

add_test(
    NAME runUnitTests
    COMMAND runUnitTests
)

请注意,我选择链接 gtest_main 而不是在 cpp 文件末尾提供 main,因为我相信这将使我能够更轻松地将测试扩展到多个文件.

Note that I have chosen to link against gtest_main instead of providing the main at the end of the cpp file as I believe this will allow me to scale testing up more easily to multiple files.

在构建生成的 .sln 文件(在 Visual C++ 2010 Express 中)时,不幸的是我得到了一长串表单错误

When building the generated .sln file (in Visual C++ 2010 Express) I unfortunately get a long list of errors of the form

2>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: virtual __thiscall std::basic_iostream<char,struct std::char_traits<char> >::~basic_iostream<char,struct std::char_traits<char> >(void)" (??1?$basic_iostream@DU?$char_traits@D@std@@@std@@UAE@XZ) already defined in gtestd.lib(gtest-all.obj)

我认为这意味着我没有成功链接到 gtest 库.我确保在链接调试库时,我尝试在调试模式下构建.

which I think means that I'm not successfully linking to the gtest libraries. I have made sure that when linking against the debug libraries, I have then tried to build in debug mode.

编辑

进行了更多的挖掘之后,我认为我的问题与我在其中构建 gtest 的库类型有关.使用 CMake 构建 gtest 时,如果未选中 BUILD_SHARED_LIBS,并且我将我的程序链接到这些 .lib 文件,则会出现上述错误.但是,如果 BUILD_SHARED_LIBS 被选中,那么我会生成一组 .lib 和 .dll 文件.当现在链接到这些 .lib 文件时,程序会编译,但在运行时抱怨它找不到 gtest.dll.

Having done some more digging, I think my issue is something to do with the type of library I am building gtest into. When building gtest with CMake, if BUILD_SHARED_LIBS is un-checked, and I link my program against these .lib files I get the errors mentioned above. However, if BUILD_SHARED_LIBS is checked then I produce a set of .lib and .dll files. When now linking against these .lib files the program compiles, but when run complains that it can't find gtest.dll.

SHARED 和非 SHARED 库之间有什么区别,如果我选择不共享,为什么它不起作用?CMakeLists.txt 中是否有我缺少的项目的选项?

What are the differences between a SHARED and a not SHARED library, and if I choose not shared, why doesn't it work? Is there an option in the CMakeLists.txt for my project that I am missing?

推荐答案

该解决方案涉及将 gtest 源目录作为项目的子目录.如果对任何人有帮助,我已经在下面包含了有效的 CMakeLists.txt.

The solution involved putting the gtest source directory as a subdirectory of your project. I've included the working CMakeLists.txt below if it is helpful to anyone.

cmake_minimum_required(VERSION 2.6)
project(basic_test)

################################
# GTest
################################
ADD_SUBDIRECTORY (gtest-1.6.0)
enable_testing()
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

################################
# Unit Tests
################################
# Add test cpp file
add_executable( runUnitTests testgtest.cpp )
# Link test executable against gtest & gtest_main
target_link_libraries(runUnitTests gtest gtest_main)
add_test( runUnitTests runUnitTests )

这篇关于如何开始使用 GTest 和 CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆