与柯南一起安装的 GTest:未定义的参考
GTest installed with Conan: undefined reference
我尝试使用通过柯南安装的 gtest,但最终出现未定义的引用链接器错误。这个问题或多或少是对 的跟进。但我认为提供的例子太简单了。我使用 gcc 6.3 在最新的 arch linux x64 下编译。
会不会是C++版本不匹配?或者您对如何解决问题有任何其他想法吗?
我将在下面提供我的源代码:
目录树:
tree
.
├── CMakeLists.txt
├── conanfile.txt
└── main.cpp
main.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, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
CMakeLists.txt:
project(ConanGtestExample)
cmake_minimum_required(VERSION 2.8.12)
set(CMAKE_CXX_STANDARD 11)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
# Necessary to compile gtest
# - dependencies and final build
# need to be compiled with same
# build type. Otherwise linker
# error will occure.
set(CMAKE_BUILD_TYPE Release)
add_executable(main main.cpp)
target_link_libraries(main ${CONAN_LIBS})
conanfile.txt:
[requires]
gtest/1.7.0@lasote/stable
[generators]
cmake
我尝试使用以下命令构建项目:
mkdir build
cd build
conan install -s build_type=Release .. --build=missing
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build .
未定义的参考输出:
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable bin/main
CMakeFiles/main.dir/main.cpp.o: In function `TestFixture_shouldCompile_Test::TestBody()':
main.cpp:(.text+0x99): undefined reference to `testing::internal::GetBoolAssertionFailureMessage[abi:cxx11](testing::AssertionResult const&, char const*, char const*, char const*)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:95: bin/main] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
我找到了问题的答案:
问题是 conan download/compile gtest 二进制文件
默认情况下 libstdc++
即使我的编译器(gcc 6.3)使用
libstdc++11
默认。因此 libstdc++
之间存在 不匹配
和 libstdc++11
.
要解决此问题,您必须明确告诉柯南使用 libstdc++11 进行编译:
conan install .. --build missing -s compiler=gcc -s compiler.version=6.3 -s compiler.libcxx=libstdc++11
我最终不得不在项目的 conanfile.py
中添加 self.options['gtest'].shared = True
来解决这个问题。之前由于某些 windows-related 原因将其设置为 false,现在变成了 non-relevant。
尝试更改为 gtest/gmock 的共享库,如果您像我一样发现默认设置已经 libstdc++11
,因此更改 conan install
参数是不够的。
我尝试使用通过柯南安装的 gtest,但最终出现未定义的引用链接器错误。这个问题或多或少是对
会不会是C++版本不匹配?或者您对如何解决问题有任何其他想法吗?
我将在下面提供我的源代码:
目录树:
tree
.
├── CMakeLists.txt
├── conanfile.txt
└── main.cpp
main.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, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
CMakeLists.txt:
project(ConanGtestExample)
cmake_minimum_required(VERSION 2.8.12)
set(CMAKE_CXX_STANDARD 11)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
# Necessary to compile gtest
# - dependencies and final build
# need to be compiled with same
# build type. Otherwise linker
# error will occure.
set(CMAKE_BUILD_TYPE Release)
add_executable(main main.cpp)
target_link_libraries(main ${CONAN_LIBS})
conanfile.txt:
[requires]
gtest/1.7.0@lasote/stable
[generators]
cmake
我尝试使用以下命令构建项目:
mkdir build
cd build
conan install -s build_type=Release .. --build=missing
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build .
未定义的参考输出:
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable bin/main
CMakeFiles/main.dir/main.cpp.o: In function `TestFixture_shouldCompile_Test::TestBody()':
main.cpp:(.text+0x99): undefined reference to `testing::internal::GetBoolAssertionFailureMessage[abi:cxx11](testing::AssertionResult const&, char const*, char const*, char const*)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:95: bin/main] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
我找到了问题的答案:
问题是 conan download/compile gtest 二进制文件
默认情况下 libstdc++
即使我的编译器(gcc 6.3)使用
libstdc++11
默认。因此 libstdc++
之间存在 不匹配
和 libstdc++11
.
要解决此问题,您必须明确告诉柯南使用 libstdc++11 进行编译:
conan install .. --build missing -s compiler=gcc -s compiler.version=6.3 -s compiler.libcxx=libstdc++11
我最终不得不在项目的 conanfile.py
中添加 self.options['gtest'].shared = True
来解决这个问题。之前由于某些 windows-related 原因将其设置为 false,现在变成了 non-relevant。
尝试更改为 gtest/gmock 的共享库,如果您像我一样发现默认设置已经 libstdc++11
,因此更改 conan install
参数是不够的。