首页 > 解决方案 > 在 LINUX 中构建 Gtest 框架

问题描述

我已经下载了 GTest 源代码 googletest-release-1.8.0.tar.gz (来自 : github )并提取了它。(在 CentOS 上工作)之后我运行了以下命令:

  1. cd googletest-release-1.8.0\googletest。
  2. cmake CMakeLists.txt。输出: -- 配置完成 -- 生成完成 -- 构建文件已写入:gtestframework/googletest-release-1.8.0/googletest

  3. 制作。输出:[50%] 构建目标 gtest [100%] 构建目标 gtest_main

在此之后,我将文件 libgtest_main.a 和 libgtest.a 复制到 /usr/lib 中。

  1. cd googletest-release-1.8.0\googlemock\gtest。
  2. 将 libgtest_main.so libgtest.so libGTest.so 复制到 /usr/lib 文件夹中。
    1. cd gtestframework\Testcode //{Testcode中的2个C++文件}
    2. cmake CMakeLists.txt。
    3. 制作。

我收到了这个错误:

Linking CXX executable runTests
CMakeFiles/runTests.dir/tests.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
tests.cpp:(.text+0x95a): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
tests.cpp:(.text+0xa14): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CMakeFiles/runTests.dir/tests.cpp.o: In function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, double>(char const*, char const*, int const&, double const&)':
tests.cpp:(.text._ZN7testing8internal18CmpHelperEQFailureIidEENS_15AssertionResultEPKcS4_RKT_RKT0_[_ZN7testing8internal18CmpHelperEQFailureIidEENS_15AssertionResultEPKcS4_RKT_RKT0_]+0x6c): undefined reference to `testing::internal::EqFailure(char const*, char const*, std::string const&, std::string const&, bool)'
CMakeFiles/runTests.dir/tests.cpp.o: In function `testing::AssertionResult testing::internal::CmpHelperEQFailure<double, double>(char const*, char const*, double const&, double const&)':
tests.cpp:(.text._ZN7testing8internal18CmpHelperEQFailureIddEENS_15AssertionResultEPKcS4_RKT_RKT0_[_ZN7testing8internal18CmpHelperEQFailureIddEENS_15AssertionResultEPKcS4_RKT_RKT0_]+0x6c): undefined reference to `testing::internal::EqFailure(char const*, char const*, std::string const&, std::string const&, bool)'
collect2: error: ld returned 1 exit status
make[2]: *** [runTests] Error 1
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
make: *** [all] Error 2

CMakeLists.txt 文件内容:

cmake_minimum_required(VERSION 2.6)

# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests tests.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)

标签: c++cmakegoogletestgooglemock

解决方案


推荐阅读