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

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

问题描述

我最近使用CMake出售了编译我的C ++项目,现在想开始为我的代码编写一些单元测试。我已决定使用Google测试工具来帮助这一点,但需要一些帮助开始。



我一直在阅读各种指南,例如包括< a href =http://code.google.com/p/googletest/wiki/V1_6_Primer>底漆, IBM产品介绍和一些有关SO的问题(这里此处)以及我已失去的其他来源。我发现有很多,但不知何故我仍然有困难。



我目前正在尝试实现最基本的测试,以确认我已经编译/安装gtest权利,它不工作。唯一的源文件(testgtest.cpp)几乎完全来自上一个答案:

  #include< iostream> 

#includegtest / gtest.h

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

和我的关联CMakeLists.txt如下:

  cmake_minimum_required(VERSION 2.6)
project(basic_test)

#设置测试
enable_testing b $ b find_package(GTest REQUIRED)
include_directories($ {GTEST_INCLUDE_DIR})

#添加测试cpp文件
add_executable(runUnitTests
testgtest.cpp


#链接测试可执行文件反对gtest& gtest_main
target_link_libraries(runUnitTests $ {GTEST_LIBRARY_DEBUG} $ {GTEST_MAIN_LIBRARY_DEBUG})

add_test(
NAME runUnitTests
COMMAND runUnitTests

<请注意,我已经选择链接到gtest_main,而不是在cpp文件的末尾提供main,因为我相信这将允许



在构建生成的.sln文件时(在Visual C ++ 2010 Express中),我遗憾地得到一个很长的错误列表形式

  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 @@已在gtestd.lib(gtest-all.obj)中定义的$ @ std @@ UAE @ XZ)

我认为这意味着我没有成功链接到gtest库。



EDIT



做了一些挖掘,我想我的问题是与我建立的图书馆类型有关。当建立gtest与CMake,如果 BUILD_SHARED_LIBS 未选中,我链接我的程序对这些.lib文件我得到上面提到的错误。但是,如果选中 BUILD_SHARED_LIBS ,那么我会生成一组.lib和.dll文件。当现在链接到这些.lib文件的程序编译,但是当运行时抱怨它找不到gtest.dll。



SHARED 和不 SHARED 库,如果我选择不共享,为什么不工作?

解决方案

解决方案涉及将gtest源目录作为项目的子目录。我已经包括工作CMakeLists.txt下面,如果它对任何人有帮助。

  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})

###############################
#单元测试
###############################
#添加测试cpp文件
add_executable(runUnitTests testgtest.cpp)
#链接测试可执行文件反对gtest& gtest_main
target_link_libraries(runUnitTests gtest gtest_main)
add_test(runUnitTests runUnitTests)


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.

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.

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);
}

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
)

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.

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)

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.

EDIT

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.

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?

解决方案

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天全站免登陆