googletest& cmake - 体系结构的未定义符号x86_64

时间:2017-01-03 22:16:14

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

我正在开发一个简单的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

1 个答案:

答案 0 :(得分: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})

我的例子没有包括让我离开的这一步。

感谢您的帮助。