colan biemer
colan biemer

Reputation: 39

googletest & cmake - Undefined symbols for architecture x86_64

I'm working on a simple c++ project and I wanted to use unit testing, specifically googletest. To get started, I based the structure of my project on https://github.com/snikulov/google-test-examples. When I cloned it and tested it, it worked perfectly. I then moved the required components to my project and I get the following linker error:

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)

I've been working through this for a few hours with a day or two in between and have had no luck. My project can be found at https://github.com/bi3mer/Bragi. I've been playing around with the cmake configuration for a while I tried running cmake like so:

cmake -DBUILD_SHARED_LIBS=ON ..

and the regular:

cmake ..

A particularly odd note that is throwing me off is that if I have my test code like so:

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

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

It will work fine. It is only once I try to instantiate my class that there is an error.

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

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

I've also looked other threads such as gtest: Undefined symbols for architecture x86_64 error with clang++ and std::vector but I have no idea where the configure script is coming from.

Based on everything that I've found it looks like there is a compiler mismatch going on, I'm just not sure how to fix it based on everything I've read.

I hope you can help, to clone:

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

I figure this is easier than pasting in the files and everying. I hope you can help, I've included the output of when I try to build below. Thank you for the help.

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

Upvotes: 3

Views: 2246

Answers (1)

colan biemer
colan biemer

Reputation: 39

Full file can be seen on github: https://github.com/bi3mer/Bragi/blob/master/CMakeLists.txt#L27

What it boils down to is adding the src

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

and then including it into the executable

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

The example I had didn't include this one step which threw me off.

Thanks for the help.

Upvotes: 1

Related Questions