2017-01-03 17 views
1

私はシンプルなC++プロジェクトに取り組んでおり、単体テスト、特にgoogletestを使いたいと思っていました。まず、私のプロジェクトの構造はhttps://github.com/snikulov/google-test-examplesです。私がそれをクローンしてそれをテストしたとき、それは完全に機能しました。私はその後、私のプロジェクトに必要なコンポーネントを移動し、私は次のリンカエラーを取得:googletest&cmake - アーキテクチャx86_64の未定義シンボル

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などの他のスレッドを見てきたが、私はconfigureスクリプトから来ている見当がつかない。

コンパイラの不一致が起こっているように見えたすべてに基づいて、私は読んだことのあるものすべてに基づいて修正する方法がわかりません。

私はクローンを作成する、あなたが役立つことを願っ:

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

私は、これはファイルやeveryingに貼り付けるよりも簡単である把握。私はあなたが助けてくれることを願っています。お手伝いありがとう。

./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 
+0

CMakeLists.txtのどこにでもsrc/Vector.cppへの参照が見つかりません。あなたのテストは明らかにそれに依存しています。 – Frank

+0

@Frank私はhttps://github.com/bi3mer/Bragi/blob/master/CMakeLists.txt#L27で遊んだことがありますが、正しくプロジェクトに取り込む方法を見つけることができませんでした。私はまた、私は使用していたGoogleのテストの例では、srcディレクトリのファイルが空であることに気づいたので、これは間違いなく正しい場所のように見えます。 –

答えて

1

全ファイルがgithubの上で見ることができます:それは、SRCに

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

を追加し、実行可能

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

にそれを含めているに沸く何https://github.com/bi3mer/Bragi/blob/master/CMakeLists.txt#L27

私が持っていた例は、私を捨てたこの一歩を含んでいませんでした。

ありがとうございました。

+0

これは私のリンカエラーを修正しましたが、gtestsではなくmain実行可能ファイルをコンパイルします: – CpILL

関連する問題