【问题标题】:googletest & cmake - Undefined symbols for architecture x86_64googletest & cmake - 架构 x86_64 的未定义符号
【发布时间】:2017-05-18 02:53:18
【问题描述】:

我正在开发一个简单的 c++ 项目,我想使用单元测试,特别是 googletest。首先,我的项目结构基于https://github.com/snikulov/google-test-examples。当我克隆它并对其进行测试时,它运行良好。然后我将所需的组件移动到我的项目中,我收到以下链接器错误:

Undefined symbols for architecture x86_64:
  "Vector::Vector(int, float*)", referenced from:
      cpp_vector_test_vector_operator_adder_Test::TestBody() in cpp_vector_test.cc.o
  "Vector::~Vector()", referenced from:
      cpp_vector_test_vector_operator_adder_Test::TestBody() in cpp_vector_test.cc.o
  "Vector::operator==(Vector)", referenced from:
      cpp_vector_test_vector_operator_adder_Test::TestBody() in cpp_vector_test.cc.o
  "Vector::operator+(Vector)", referenced from:
      cpp_vector_test_vector_operator_adder_Test::TestBody() in cpp_vector_test.cc.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我已经为此工作了几个小时,中间有一两天,但没有运气。我的项目可以在https://github.com/bi3mer/Bragi 找到。我一直在玩 cmake 配置一段时间,我尝试像这样运行 cmake:

cmake -DBUILD_SHARED_LIBS=ON ..

和常规:

cmake ..

让我失望的一个特别奇怪的注释是,如果我的测试代码是这样的:

#include "Vector.h"
#include "gtest/gtest.h"

TEST(cpp_vector_test, vector_operator_adder)
{
    EXPECT_EQ(true, true);
}

它会正常工作的。只有在我尝试实例化我的类时才会出现错误。

#include "Vector.h"
#include "gtest/gtest.h"

TEST(cpp_vector_test, vector_operator_adder)
{
    Vector test = Vector(3); // throws error
    EXPECT_EQ(true, true);
} 

我还查看了其他线程,例如 gtest: Undefined symbols for architecture x86_64 error with clang++ and std::vector,但我不知道配置脚本的来源。

根据我发现的所有内容,似乎存在编译器不匹配,我只是不确定如何根据我阅读的所有内容来修复它。

希望你能帮忙,克隆:

git clone https://github.com/bi3mer/Bragi.git

我认为这比粘贴文件更容易。我希望你能提供帮助,我已经包含了我尝试在下面构建时的输出。谢谢你的帮助。

./build.sh 
-- The C compiler identification is AppleClang 8.0.0.8000042
-- The CXX compiler identification is AppleClang 8.0.0.8000042
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/colan/Documents/work/Bragi/build
Scanning dependencies of target googletest
[ 10%] Creating directories for 'googletest'
[ 20%] Performing download step (git clone) for 'googletest'
Cloning into 'googletest'...
Already on 'master'
Your branch is up-to-date with 'origin/master'.
[ 30%] No patch step for 'googletest'
[ 40%] Performing update step for 'googletest'
Current branch master is up to date.
[ 50%] Performing configure step for 'googletest'
-- The C compiler identification is AppleClang 8.0.0.8000042
-- The CXX compiler identification is AppleClang 8.0.0.8000042
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: //anaconda/bin/python (found version "2.7.12") 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG
    CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE


-- Build files have been written to: /Users/colan/Documents/work/Bragi/build/ext/gtest/src/googletest-build
[ 60%] Performing build step for 'googletest'
Scanning dependencies of target gmock
[  9%] Building CXX object googlemock/CMakeFiles/gmock.dir/__/googletest/src/gtest-all.cc.o
[ 18%] Building CXX object googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.o
[ 27%] Linking CXX static library libgmock.a
[ 27%] Built target gmock
Scanning dependencies of target gmock_main
[ 36%] Building CXX object googlemock/CMakeFiles/gmock_main.dir/__/googletest/src/gtest-all.cc.o
[ 45%] Building CXX object googlemock/CMakeFiles/gmock_main.dir/src/gmock-all.cc.o
[ 54%] Building CXX object googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
[ 63%] Linking CXX static library libgmock_main.a
[ 63%] Built target gmock_main
Scanning dependencies of target gtest
[ 72%] Building CXX object googlemock/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 81%] Linking CXX static library libgtest.a
[ 81%] Built target gtest
Scanning dependencies of target gtest_main
[ 90%] Building CXX object googlemock/gtest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[100%] Linking CXX static library libgtest_main.a
[100%] Built target gtest_main
[ 70%] No install step for 'googletest'
[ 80%] Completed 'googletest'
[ 80%] Built target googletest
Scanning dependencies of target Bragi_test
[ 90%] Building CXX object CMakeFiles/Bragi_test.dir/test/cpp_vector_test.cc.o
[100%] Linking CXX executable Bragi_test
Undefined symbols for architecture x86_64:
  "Vector::Vector(int, float*)", referenced from:
      cpp_vector_test_vector_operator_adder_Test::TestBody() in cpp_vector_test.cc.o
  "Vector::~Vector()", referenced from:
      cpp_vector_test_vector_operator_adder_Test::TestBody() in cpp_vector_test.cc.o
  "Vector::operator==(Vector)", referenced from:
      cpp_vector_test_vector_operator_adder_Test::TestBody() in cpp_vector_test.cc.o
  "Vector::operator+(Vector)", referenced from:
      cpp_vector_test_vector_operator_adder_Test::TestBody() in cpp_vector_test.cc.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [Bragi_test] Error 1
make[1]: *** [CMakeFiles/Bragi_test.dir/all] Error 2
make: *** [all] Error 2
UpdateCTestConfiguration  from :/Users/colan/Documents/work/Bragi/build/DartConfiguration.tcl
UpdateCTestConfiguration  from :/Users/colan/Documents/work/Bragi/build/DartConfiguration.tcl
Test project /Users/colan/Documents/work/Bragi/build
Constructing a list of tests
Done constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end
test 1
    Start 1: test1
Could not find executable Bragi_test
Looked in the following places:
Bragi_test
Bragi_test
Release/Bragi_test
Release/Bragi_test
Debug/Bragi_test
Debug/Bragi_test
MinSizeRel/Bragi_test
MinSizeRel/Bragi_test
RelWithDebInfo/Bragi_test
RelWithDebInfo/Bragi_test
Deployment/Bragi_test
Deployment/Bragi_test
Development/Bragi_test
Development/Bragi_test

1: Test command: 
Unable to find executable: Bragi_test
1/1 Test #1: test1 ............................***Not Run   0.00 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.00 sec

The following tests FAILED:
      1 - test1 (Not Run)
Errors while running CTest

【问题讨论】:

  • 我在 CMakeLists.txt 的任何地方都找不到对 src/Vector.cpp 的任何引用。您的测试显然取决于它。
  • @Frank 我玩过github.com/bi3mer/Bragi/blob/master/CMakeLists.txt#L27,但我还没有找到一种方法将它正确地放入项目中。我还刚刚注意到,在我使用的 google 测试示例中,src 目录中的文件是空的,所以这绝对看起来是正确的地方。

标签: c++ unit-testing linker x86 googletest


【解决方案1】:

完整文件可见github:https://github.com/bi3mer/Bragi/blob/master/CMakeLists.txt#L27

归结为添加 src

file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.cc)

然后将其包含到可执行文件中

add_executable(${PROJECT_TEST_NAME} ${TEST_SRC_FILES} ${SRC_FILES})

我的示例没有包含让我失望的这一步骤。

感谢您的帮助。

【讨论】:

  • 这为我修复了链接器错误,但它只编译了主可执行文件而不是 gtests :(
猜你喜欢
  • 2016-12-12
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 2012-07-20
  • 1970-01-01
  • 2015-08-23
  • 2014-05-27
相关资源
最近更新 更多