C++ GTest链接错误:未定义对';foo::函数';

C++ GTest链接错误:未定义对';foo::函数';,c++,c++11,cmake,googletest,undefined-reference,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,并得到了工作的例子。现在我想测试我自己的代码,但是我遇到了一个链接问题

我搜索了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)
测试。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();
}
#包括“DataHandler.h”
#包括
测试(已填支票,全部){
数据处理程序(“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,字符**argv){
测试:InitGoogleTest(&argc,argv);
返回运行所有测试();
}
我得到的错误是:

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
cmakfiles/runTests.dir/tests.cpp.o:在函数“checkFilled\u All\u Test::TestBody()”中:
tests.cpp:(.text+0x121):未定义对“DataHandler::DataHandler(std:_cxx11::basic_string,std:_cxx11::basic_string,unsigned char)”的引用
tests.cpp:(.text+0x172):对“DataHandler::checkFilled(unsigned char)”的未定义引用
tests.cpp:(.text+0x271):对“DataHandler::checkFilled(unsigned char)”的未定义引用
tests.cpp:(.text+0x382):对“DataHandler::checkFilled(unsigned char)”的未定义引用
tests.cpp:(.text+0x48d):对“DataHandler::checkFilled(unsigned char)”的未定义引用
collect2:错误:ld返回了1个退出状态
CMakeFiles/runTests.dir/build.make:95:目标“runTests”的配方失败
生成[2]:***[runTests]错误1
CMakeFiles/Makefile2:67:目标“CMakeFiles/runTests.dir/all”的配方失败
生成[1]:***[CMakeFiles/runTests.dir/all]错误2
Makefile:83:目标“全部”的配方失败
make:**[全部]错误2

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

您的CMake项目中有几点:

SET(CMAKE_CXX_FLAGS "-std=gnu++11")
使用
CMAKE\u CXX\u标志时,最好不要擦除以前的值:

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
默认情况下,该值为空,因此此处不应更改任何内容。不管怎样,为了要求C++ 11,你可以改为<代码> cMax.Cxxx标准< /代码>:

SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
下一步:

如果使用
GLOB
,则应该传递文件模式,而不是文件路径。无论如何,从:

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

请考虑设置

set(files
    Datahandler.h 
    Datahandler.cpp 
    )
下一步:

而不是旧式的<代码> ${GestBooList} /Cuth>,您可以考虑:

GTest::Main
将通过导入的目标处理include目录,因此您无需调用
include_目录
。此外,它将向线程库添加一个依赖项,因此我确信您可以删除
pthread
(待测试)。最后,
GTest::Main
包含Main函数的基本实现,它与您所做的完全相同,因此您可以删除您的函数

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

cmake_minimum_required(VERSION 2.8.11)

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

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)
test.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));
}
#包括“DataHandler.h”
#包括
测试(已填支票,全部){
数据处理程序(“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));
}

有史以来最伟大的答案之一
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)
#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));
}