Ubuntu 20.10 上的 GTest 链接器错误
GTest linker errors on Ubuntu 20.10
我在 Ubuntu 20.04 上有以下错误:
[1/1] Linking CXX executable bin/project_2022_matrix_tests
FAILED: bin/project_2022_matrix_tests
: && /usr/bin/c++ -g CMakeFiles/project_2022_matrix_tests.dir/CException.cpp.o CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o CMakeFiles/project_2022_matrix_tests.dir/tests/main_unit_tests.cpp.o -o bin/project_2022_matrix_tests -L/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/lib -Wl,-rpath,/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/lib -lgtest_main -lgmock_main -lgmock -lgtest -lpthread -lpthread && :
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult::AppendMessage(testing::Message const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:357 : undefined reference to « testing::Message::GetString[abi:cxx11]() const »
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult testing::internal::CmpHelperEQFailure<CException*, decltype(nullptr)>(char const*, char const*, CException* const&, decltype(nullptr) const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:1529 : undefined reference to « testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) »
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult testing::internal::CmpHelperEQFailure<char*, decltype(nullptr)>(char const*, char const*, char* const&, decltype(nullptr) const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:1529 : undefined reference to « testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) »
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:1529 : undefined reference to « testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) »
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.21)
project(project_2022_matrix_tests)
set(CMAKE_CXX_STANDARD 14)
if(EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
else()
message(WARNING "The file conanbuildinfo.cmake doesn't exist, you have to run conan install first")
endif()
find_package(GTest REQUIRED)
include_directories(.)
enable_testing()
add_executable(project_2022_matrix_tests
CException.cpp
CException.h
CMatrix.cpp
CMatrix.h
CIndexableMatrix.h
main.cpp)
add_executable(project_2022_matrix_tests_tests
CException.cpp
CException.h
tests/CException_unit_tests.cpp tests/main_unit_tests.cpp tests/CMatrix_unit_tests.cpp)
target_link_libraries(project_2022_matrix_tests_tests gtest)
我不得不说这适用于 macOS 上的 CLion,但出于某些原因,不适用于 Ubuntu...
如果您need/want需要更多信息,请询问。
感谢您以后的回复。
要使用作为评论发布的解决方案,只需编辑 .conan/profiles/default
并更改行 compiler.libcxx
以将值设置为 libstdc++11
。
我在 Ubuntu 20.04 上有以下错误:
[1/1] Linking CXX executable bin/project_2022_matrix_tests
FAILED: bin/project_2022_matrix_tests
: && /usr/bin/c++ -g CMakeFiles/project_2022_matrix_tests.dir/CException.cpp.o CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o CMakeFiles/project_2022_matrix_tests.dir/tests/main_unit_tests.cpp.o -o bin/project_2022_matrix_tests -L/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/lib -Wl,-rpath,/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/lib -lgtest_main -lgmock_main -lgmock -lgtest -lpthread -lpthread && :
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult::AppendMessage(testing::Message const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:357 : undefined reference to « testing::Message::GetString[abi:cxx11]() const »
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult testing::internal::CmpHelperEQFailure<CException*, decltype(nullptr)>(char const*, char const*, CException* const&, decltype(nullptr) const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:1529 : undefined reference to « testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) »
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult testing::internal::CmpHelperEQFailure<char*, decltype(nullptr)>(char const*, char const*, char* const&, decltype(nullptr) const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:1529 : undefined reference to « testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) »
/usr/bin/ld : CMakeFiles/project_2022_matrix_tests.dir/tests/CException_unit_tests.cpp.o : in the function « testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&) » :
/home/username/.conan/data/gtest/cci.20210126/_/_/package/71c983c52942eb4756e4bd60e4cbec9fd7557e5d/include/gtest/gtest.h:1529 : undefined reference to « testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) »
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.21)
project(project_2022_matrix_tests)
set(CMAKE_CXX_STANDARD 14)
if(EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
else()
message(WARNING "The file conanbuildinfo.cmake doesn't exist, you have to run conan install first")
endif()
find_package(GTest REQUIRED)
include_directories(.)
enable_testing()
add_executable(project_2022_matrix_tests
CException.cpp
CException.h
CMatrix.cpp
CMatrix.h
CIndexableMatrix.h
main.cpp)
add_executable(project_2022_matrix_tests_tests
CException.cpp
CException.h
tests/CException_unit_tests.cpp tests/main_unit_tests.cpp tests/CMatrix_unit_tests.cpp)
target_link_libraries(project_2022_matrix_tests_tests gtest)
我不得不说这适用于 macOS 上的 CLion,但出于某些原因,不适用于 Ubuntu... 如果您need/want需要更多信息,请询问。
感谢您以后的回复。
要使用作为评论发布的解决方案,只需编辑 .conan/profiles/default
并更改行 compiler.libcxx
以将值设置为 libstdc++11
。