【发布时间】: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