C++ Gtest:未定义的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12657596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:29:13  来源:igfitidea点击:

Gtest: Undefined References

c++unit-testingunixcmakegoogletest

提问by Vance

I am trying to use GoogleTest to test a simple function, but as I run makein my build folder, the compiler throws Undefined Referenceerror messages at me. I've referenced the gtest header file, so I'm not sure what is wrong. Any ideas? I'm new to the entire subject of both unix and unit testing , so I could very well be missing something simple. Thanks in advance!

我正在尝试使用 GoogleTest 来测试一个简单的函数,但是当我make在构建文件夹中运行时,编译器Undefined Reference向我抛出错误消息。我已经引用了 gtest 头文件,所以我不确定出了什么问题。有任何想法吗?我对 unix 和 unit testing 的整个主题都不熟悉,所以我很可能会遗漏一些简单的东西。提前致谢!

Error Messages:

错误信息:

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

main.cpp

主程序

#include "gtest/gtest.h"

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

Test.cpp

测试文件

#include "gtest/gtest.h"
#include "Testable.h"

TEST(GetTwoTest, Two) {
    EXPECT_EQ(2, GetTwo());
}

Testable.cpp

可测试文件

#include "Testable.h"

int GetTwo() {
    return 3;
}

Here is my CMakeLists.txt file:

这是我的 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)

采纳答案by Fraser

Your setup looks to be almost correct. However, you're needing to have 2 separate mainfunctions; one for the real executable Proj2and another with the gtest includes and functions for the test executable unit-test.

您的设置看起来几乎是正确的。但是,您需要有 2 个独立的main功能;一个用于真正的可执行文件Proj2,另一个用于测试可执行文件的 gtest 包含和函数unit-test

You could do this by having 2 different main.cpp files, say main.cpp and test_main.cpp. The one you've shown would be test_main.cpp, and would be included in the add_executable(unit-test ...command.

你可以通过有 2 个不同的 main.cpp 文件来做到这一点,比如 main.cpp 和 test_main.cpp。您显示的将是 test_main.cpp,并将包含在add_executable(unit-test ...命令中。

Your new main.cpp would have no references to gtest, either includes or functions.

您的新 main.cpp 将没有对 gtest 的引用,无论是包含还是函数。

回答by PiotrNycz

From linker errors it is obvious that you did not link gtest library to your test program.

从链接器错误来看,很明显您没有将 gtest 库链接到您的测试程序。

See Primer:

入门

To write a test program using Google Test, you need to compile Google Test into a library and link your test with it. ...

要使用 Google Test 编写测试程序,您需要将 Google Test 编译成一个库并将您的测试与其链接。...

Just see this doc for details about your compiler and system.

有关您的编译器和系统的详细信息,请参阅此文档。