我正在尝试使用 GoogleTest 测试一个简单的函数,但是当我make
在构建文件夹中运行时,编译器会Undefined Reference
向我抛出错误消息。我已经引用了 gtest 头文件,所以我不确定出了什么问题。有任何想法吗?我对 unix 和 unit testing 的整个主题都是新手,所以我很可能会遗漏一些简单的东西。提前致谢!
错误信息:
CMakeFiles/Proj2.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)'
main.cpp:(.text+0x23): undefined reference to `testing::UnitTest::GetInstance()'
main.cpp:(.text+0x2b): undefined reference to `testing::UnitTest::Run()'
collect2: error: ld returned 1 exit status
主文件
#include "gtest/gtest.h"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
测试.cpp
#include "gtest/gtest.h"
#include "Testable.h"
TEST(GetTwoTest, Two) {
EXPECT_EQ(2, GetTwo());
}
可测试的.cpp
#include "Testable.h"
int GetTwo() {
return 3;
}
这是我的 CMakeLists.txt 文件:
cmake_minimum_required(VERSION 2.6)
SET(CMAKE_CXX_FLAGS "-std=gnu++11") #Turn on C++11 Support
set(FILES_TO_TEST Testable.cpp)
set(UNIT_TESTS Test.cpp)
set(MAIN_FILE main.cpp)
add_subdirectory(gtest) #Build all the gtest stuff
include_directories(gtest/include)
include_directories(.)
add_library(codeToTest ${FILES_TO_TEST})
add_executable(Proj2 ${MAIN_FILE})
target_link_libraries(Proj2 codeToTest)
add_executable(unit-test ${UNIT_TESTS})
target_link_libraries(unit-test gtest gtest_main rt pthread codeToTest)