我尝试使用通过 conan 安装的 gtest,但最终出现了未定义的引用链接器错误。这个问题或多或少是这个 stackoverflow 问题的后续。但我认为提供的例子很简单。我在最新的 arch linux x64 下编译,使用 gcc 6.3。
C++ 版本会不会有些不匹配?或者您对如何解决问题有任何其他想法吗?
我将在以下提供我的源代码:
目录树:
tree
.
??? CMakeLists.txt
??? conanfile.txt
??? main.cpp
Run Code Online (Sandbox Code Playgroud)
主.cpp:
#include <iostream>
#include <gtest/gtest.h>
class TestFixture : public ::testing::Test {
protected:
void SetUp(){
std::cout << "SetUp()" << std::endl;
}
void TearDown(){
std::cout << "TearDown()" << std::endl;
}
};
TEST_F (TestFixture, shouldCompile) {
std::cout << "shouldCompile" << std::endl;
ASSERT_TRUE(true); // works, maybe optimized out?
ASSERT_TRUE("hi" == "hallo"); // undefined reference
}
int main(int argc, …Run Code Online (Sandbox Code Playgroud) 我使用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 …Run Code Online (Sandbox Code Playgroud)