GTest链接错误:未定义引用' foo :: function'

时间:2016-10-25 09:03:49

标签: c++ c++11 cmake googletest undefined-reference

我想使用GTest并让这个例子起作用。现在我想测试自己的代码,但是我遇到了链接问题。

我在stackoverflow上搜索,发现了类似的问题,但答案并没有帮助我解决问题。

我的档案是:

的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();
}

我得到的错误是:

CMakeFiles/runTests.dir/tests.cpp.o: 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/runTests.dir/build.make:95: 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

我几乎可以肯定它与包含或链接问题有关,但我找不到合适的语法。

1 个答案:

答案 0 :(得分:1)

您的CMake项目有几点:

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

使用CMAKE_CXX_STANDARD时,最好不要删除以前的值:

SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)

默认情况下,该值为空,因此不应在此处更改任何内容。无论如何,要求C ++ 11,您可以考虑使用file(GLOB files Datahandler.h Datahandler.cpp )

GLOB

下一步:

set

如果使用set(files Datahandler.h Datahandler.cpp ) ,则应传递文件模式,而不是文件路径。无论如何,来自doc

  

我们不建议使用GLOB从源树中收集源文件列表。如果在添加或删除源时没有更改CMakeLists.txt文件,则生成的构建系统无法知道何时要求CMake重新生成。

请考虑include_directories(${GTEST_INCLUDE_DIRS}) #... target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)

${GTEST_LIBRARIES}

下一步:

target_link_libraries(runTests GTest::Main)

您可以考虑imported targets

而不是过时的GTest::Main
include_directories

pthread将通过导入的目标处理包含目录,因此您无需致电GTest::Main。此外,它将为线程库添加依赖项,因此我非常确定您可以删除cmake_minimum_required(VERSION 2.8.11) (待测试)。最后,cmake_minimum_required(VERSION 2.8.11) SET(CMAKE_CXX_STANDARD 11) SET(CMAKE_CXX_STANDARD_REQUIRED ON) set(files Datahandler.h Datahandler.cpp ) # Locate GTest find_package(GTest REQUIRED) # Link runTests with what we want to test and the GTest library add_executable(runTests tests.cpp ${files}) target_link_libraries(runTests GTest::Main) 包含主要功能的基本实现,它完全按照您的操作执行,因此您可以删除自己的功能。

请注意,您需要升级所需的最低版本的CMake才能使导入的目标完全可用:

test.cpp

因此,您2016年的CMake项目应如下所示:

#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));
}

while (src.hasNext()) { if (src.hasNext()) {

{{1}}