问题描述
我使用 cmake 构建我的项目,并使用柯南以安装 Google测试作为依赖项:
I use cmake to build my project and conan to install Google Test as dependency:
conanfile.txt
[requires]
gtest/1.7.0@lasote/stable
[generators]
cmake
[imports]
bin, *.dll -> ./build/bin
lib, *.dylib* -> ./build/bin
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})
test/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
}
构建
$ conan install --build=missing
$ mkdir build && cd build
$ cmake .. && cmake --build .
我可以使用ASSERT_STREQ
,但是如果使用EXPECT_FALSE
,则会出现意外错误:
I can use ASSERT_STREQ
, but if I use EXPECT_FALSE
I get an unexpected error:
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
我的配置出了什么问题?
What's wrong with my configuration?
推荐答案
问题是您正在使用默认设置(构建类型为 Release )安装conan依赖项:
The issue is that you are installing conan dependencies using the default settings (which is build type Release):
$ conan install --build=missing
# equivalent to
$ conan install -s build_type=Release ... --build=missing
默认设置可以在您的conan.conf
文件中看到
The default settings can be seen in your conan.conf
file
然后,您在默认构建类型为 Debug 的nix系统中使用cmake,这是一个单配置环境(与Visual Studio相对于多配置Debug/Release环境相反) ),因此在执行操作时:
Then, you are using cmake in a nix system with the default build type which is Debug, which is a single-conf environment (opposed to multi-configuration Debug/Release environments, as Visual Studio), so when you are doing:
$ cmake .. && cmake --build .
# equivalent to
$ cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build .
调试/发布版本的不兼容性导致该未解决的问题.因此解决方案是使用与您已安装的依赖项相匹配的相同构建类型:
The incompatibility of Debug/Release build leads to that unresolved issue. So the solution would be to use the same build type that matches your installed dependencies:
$ cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build .
如果使用像Visual Studio这样的多配置环境,则正确的方法是:
If using multi-configuration environments like Visual Studio, the correct way would be:
$ cmake .. && cmake --build . --config Release
这篇关于随柯南安装的gtest:未定义对`testing :: internal :: GetBoolAssertionFailureMessage`的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!