Gtest:未定义引用 [英] Gtest: Undefined References

查看:3793
本文介绍了Gtest:未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用GoogleTest测试一个简单的函数,但因为我在我的构建文件夹中运行 make ,编译器抛出 Undefined Reference 错误消息在我。我引用了gtest头文件,所以我不知道是什么问题。有任何想法吗?我是unix和单元测试的整个主题的新手,所以我可以很好地缺少一些简单的东西。提前感谢!



错误讯息:

  CMakeFiles / Proj2。 dir / main.cpp.o:在main函数中:
main.cpp :(。text + 0x1e):未定义的引用`testing :: InitGoogleTest(int *,char **)'
main.cpp :(。text + 0x23):未定义的引用`testing :: UnitTest :: GetInstance()'
main.cpp :( .text + 0x2b):undefined引用`testing :: UnitTest :: Run()'
collect2:error:ld返回1退出状态

main.cpp

  #includegtest / gtest.h

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

Test.cpp

  #includegtest / gtest.h
#includeTestable.h

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

Testable.cpp

  #includeTestable.h

int GetTwo(){
return 3;
}

这是我的CMakeLists.txt文件:

  cmake_minimum_required(VERSION 2.6)

SET(CMAKE_CXX_FLAGS-std = gnu ++ 11)#打开C ++ 11支持

set(FILES_TO_TEST Testable.cpp)
set(UNIT_TESTS Test.cpp)
set(MAIN_FILE main.cpp)

add_subdirectory(gtest)构建所有的gtest东西
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)


解决方案

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

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



您的新main.cpp没有对gtest的引用,包括或函数。


I am trying to use GoogleTest to test a simple function, but as I run make in my build folder, the compiler throws Undefined Reference error 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!

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:

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)

解决方案

Your setup looks to be almost correct. However, you're needing to have 2 separate main functions; one for the real executable Proj2 and another with the gtest includes and functions for the test executable 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.

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

这篇关于Gtest:未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆