【问题标题】:undefined reference to `testing::UnitTest::Run()对`testing::UnitTest::Run() 的未定义引用
【发布时间】:2021-05-23 15:09:51
【问题描述】:

The different repository images

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。请记住,我在test_labyrinthe.cpp 中只有一行#include <gtest/gtest.h>

当我运行 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

【问题讨论】:

  • gtest_main 使用来自 gtest 的函数。因此,gtest_main 应该在链接器命令行中之前 gtest。但您的订单相反:set(GTEST_LIBRARIES pthread gtest gmock gtest_main gmock_main).
  • @Tsyvarev 我试过了,但似乎没有帮助。仍然有同样的问题。我使用过 VS Code。

标签: c++ unit-testing cmake googletest


【解决方案1】:

我认为根本问题是:

请记住,我在test_labyrinthe.cpp 中只有一行#include <gtest/gtest.h>

您需要在文件中添加一个实际的测试用例。

test_labyrinthe.cpp:

#include <gtest/gtest.h>

TEST(SomeTest, ABasicTest) {
  EXPECT_EQ(1, 1);
}

一旦你这样做了,你可能会遇到一些 pthread 链接器错误。在 gtest 之后更改为链接 pthread 应该可以解决该问题。

set(GTEST_LIBRARIES gtest gmock gtest_main gmock_main pthread)

【讨论】:

    【解决方案2】:

    经过无数次尝试后,解决方案是添加-pthread,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-08
      • 2014-09-09
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2022-01-22
      相关资源
      最近更新 更多