mai*_*mic 2 c++ unit-testing googletest conan
我使用cmake来构建我的项目并使用conan来安装Google Test作为依赖项:
配置文件.txt
[requires]
gtest/1.7.0@lasote/stable
[generators]
cmake
[imports]
bin, *.dll -> ./build/bin
lib, *.dylib* -> ./build/bin
Run Code Online (Sandbox Code Playgroud)
CMakeLists.txt
PROJECT(MyTestingExample)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
INCLUDE(conanbuildinfo.cmake)
CONAN_BASIC_SETUP()
ADD_EXECUTABLE(my_test test/my_test.cpp)
TARGET_LINK_LIBRARIES(my_test ${CONAN_LIBS})
Run Code Online (Sandbox Code Playgroud)
测试/my_test.cpp
#include <gtest/gtest.h>
#include <string>
TEST(MyTest, foobar) {
std::string foo("foobar");
std::string bar("foobar");
ASSERT_STREQ(foo.c_str(), bar.c_str()); // working
EXPECT_FALSE(false); // error
}
Run Code Online (Sandbox Code Playgroud)
建造
$ conan install --build=missing
$ mkdir build && cd build
$ cmake .. && cmake --build .
Run Code Online (Sandbox Code Playgroud)
我可以使用ASSERT_STREQ,但如果我使用,EXPECT_FALSE我会收到一个意外错误:
my_test.cpp:(.text+0x1e1): undefined reference to `testing::internal::GetBoolAssertionFailureMessage[abi:cxx11](testing::AssertionResult const&, char const*, char const*, char const*)'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我的配置有什么问题?
问题是您正在使用默认设置(构建类型Release)安装 conan 依赖项:
$ conan install --build=missing
# equivalent to
$ conan install -s build_type=Release ... --build=missing
Run Code Online (Sandbox Code Playgroud)
可以在您的conan.conf文件中看到默认设置
然后,您在 nix 系统中使用 cmake,默认构建类型为Debug,这是一个单配置环境(与多配置调试/发布环境相反,如 Visual Studio),因此当您执行以下操作时:
$ cmake .. && cmake --build .
# equivalent to
$ cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build .
Run Code Online (Sandbox Code Playgroud)
调试/发布版本的不兼容导致了这个未解决的问题。因此,解决方案是使用与您安装的依赖项匹配的相同构建类型:
$ cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build .
Run Code Online (Sandbox Code Playgroud)
如果使用像 Visual Studio 这样的多配置环境,正确的方法是:
$ cmake .. && cmake --build . --config Release
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5420 次 |
| 最近记录: |