mardi 25 octobre 2016

GTest linking error: undefined reference to 'foo::function'

I want to use GTest and got the example to work. Now I want to test my own code, but I get a linking problem.

I've search on stackoverflow, and found a similar problem but the answers didn't help me to fix my problem.

My files are:

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

SET(CMAKE_CXX_FLAGS "-std=gnu++11")

# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
file(GLOB files
    Datahandler.h 
    Datahandler.cpp 
    )

# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests tests.cpp ${files} )
target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)

tests.cpp

#include "DataHandler.h"
#include <gtest/gtest.h>

TEST(checkFilled, All){
    DataHandler handler("loc", "prot", 1);
    handler.filledElement[1] = 1;
    ASSERT_EQ(1, handler.checkFilled(1));
    ASSERT_EQ(0, handler.checkFilled(2));
    ASSERT_EQ(1, handler.checkFilled(8));
    ASSERT_EQ(0, handler.checkFilled(9));
}

int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

The errors I get are:

CMakeFiles/http://ift.tt/2eChGOL: In function `checkFilled_All_Test::TestBody()':
tests.cpp:(.text+0x121): undefined reference to `DataHandler::DataHandler(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char)'
tests.cpp:(.text+0x172): undefined reference to `DataHandler::checkFilled(unsigned char)'
tests.cpp:(.text+0x271): undefined reference to `DataHandler::checkFilled(unsigned char)'
tests.cpp:(.text+0x382): undefined reference to `DataHandler::checkFilled(unsigned char)'
tests.cpp:(.text+0x48d): undefined reference to `DataHandler::checkFilled(unsigned char)'
collect2: error: ld returned 1 exit status
CMakeFiles/http://ift.tt/2eAEXU5: recipe for target 'runTests' failed
make[2]: *** [runTests] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

I'm almost certain it has something to do with include or link problem, but I cant find the right syntax.

Aucun commentaire:

Enregistrer un commentaire