首页 > 解决方案 > 对`testing::UnitTest::Run() 的未定义引用

问题描述

不同的存储库映像

add_executable(test_labyrinthe test_labyrinthe.cpp)
add_test(test_labyrinthe.cpp test_labyrinthe)
target_link_libraries(test_labyrinthe ${GTEST_LIBRARIES})

这就是我在 Test/CMakeLists.txt 中的内容

cmake_minimum_required(VERSION 3.5)
project(TP1)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES
    Labyrinthe.cpp
    Labyrinthe.h
    Piece.cpp
    Piece.h
    Porte.cpp
    Porte.h
    Principal.cpp)

add_executable(TP1 ${SOURCE_FILES})


###########
## GTEST ##
###########

option(BUILD_TESTING "build unit tests" ON)

if(BUILD_TESTING)
  include(ExternalProject)

  ExternalProject_add(gtest-target
    GIT_REPOSITORY "https://github.com/google/googletest"
    CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/extern"
    UPDATE_COMMAND ""
  )

  include_directories(${CMAKE_CURRENT_BINARY_DIR}/extern/include)
  link_directories(${CMAKE_CURRENT_BINARY_DIR}/extern/lib)
  set(GTEST_LIBRARIES pthread gtest gmock gtest_main gmock_main)

  enable_testing()

  add_subdirectory(test)
endif()

这就是我的主要内容CMakeLists.txt。请记住,我只有#include <gtest/gtest.h>.test_labyrinthe.cpp

当我跑步时cmake ..make我得到了

[ 53%] Built target gtest-target
[ 86%] Built target TP1
Scanning dependencies of target test_labyrinthe
[ 93%] Building CXX object test/CMakeFiles/test_labyrinthe.dir/test_labyrinthe.cpp.o
[100%] Linking CXX executable test_labyrinthe
/usr/bin/ld: /home/infinity/ULaval/Session_Hiver_2021/Algorithmes_et_Structures_de_donnees/Travaux_Pratiques/TP1/CodeTP1/extern/lib/libgtest_main.a(gtest_main.cc.o): in function `main':
gtest_main.cc:(.text+0x3a): undefined reference to `testing::InitGoogleTest(int*, char**)'
/usr/bin/ld: /home/infinity/ULaval/Session_Hiver_2021/Algorithmes_et_Structures_de_donnees/Travaux_Pratiques/TP1/CodeTP1/extern/lib/libgtest_main.a(gtest_main.cc.o): in function `RUN_ALL_TESTS()':
gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x9): undefined reference to `testing::UnitTest::GetInstance()'
/usr/bin/ld: gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x11): undefined reference to `testing::UnitTest::Run()'
collect2: error: ld returned 1 exit status
make[2]: *** [test/CMakeFiles/test_labyrinthe.dir/build.make:84: test/test_labyrinthe] Error 1
make[1]: *** [CMakeFiles/Makefile2:152: test/CMakeFiles/test_labyrinthe.dir/all] Error 2
make: *** [Makefile:95: all] Error 2

这真的很奇怪。不知道如何使这项工作。你能帮我吗?

编辑当我跑步时make test,我得到了

└╼ make test
Running tests...
Test project /home/infinity/ULaval/Session_Hiver_2021/Algorithmes_et_Structures_de_donnees/Travaux_Pratiques/TP1/CodeTP1
    Start 1: test_labyrinthe.cpp
Could not find executable test_labyrinthe
Looked in the following places:
test_labyrinthe
test_labyrinthe
Release/test_labyrinthe
Release/test_labyrinthe
Debug/test_labyrinthe
Debug/test_labyrinthe
MinSizeRel/test_labyrinthe
MinSizeRel/test_labyrinthe
RelWithDebInfo/test_labyrinthe
RelWithDebInfo/test_labyrinthe
Deployment/test_labyrinthe
Deployment/test_labyrinthe
Development/test_labyrinthe
Development/test_labyrinthe
Unable to find executable: test_labyrinthe
1/1 Test #1: test_labyrinthe.cpp ..............***Not Run   0.00 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.00 sec

The following tests FAILED:
          1 - test_labyrinthe.cpp (Not Run)
Errors while running CTest
make: *** [Makefile:84: test] Error 8

标签: c++unit-testingcmakegoogletest

解决方案


经过无数次尝试后,解决方案是添加-pthread如您在此处看到的:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")


推荐阅读