【问题标题】:CMake gtest 'Error running test executable' Result:24CMake gtest'错误运行测试可执行文件'结果:24
【发布时间】:2021-12-08 03:23:35
【问题描述】:

我有一个项目,项目结构如下: -项目根

--src
----Main.cpp
----Room.cpp
----Room.h
----xxx.cpp
----xxx.h
--Tests
----Tests.cpp
----CMakeLists.txt

我的 CMakeList.txt:

cmake_minimum_required(VERSION 3.14)
enable_testing()
project(practical_08_11_Tests)
set(CMAKE_CXX_STANDARD 14)
#Setup GoogleTest Unit Testing Framework
include(FetchContent)
FetchContent_Declare(
        googletest
        URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip
)

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)


#Get ABSOLUTE PATH of SRC dir
get_filename_component(dir ../src ABSOLUTE)

#Add headers and src files
file (GLOB_RECURSE headers "${dir}/*.h")
file (GLOB_RECURSE src "${dir}/*.cpp")

#Remove Main.cpp in src
#message(STATUS "src with Main.cpp: ${src}")
get_filename_component(full_path_test_cpp ../src/Main.cpp ABSOLUTE)
#message("STATUS: Main.cpp Path: ${full_path_test_cpp}")
#Remove item in list
list(REMOVE_ITEM src "${full_path_test_cpp}")
#message(STATUS "src without Main.cpp: ${src}")

#add library
add_library (SRC STATIC ${headers} ${src})


target_include_directories(SRC PUBLIC ${dir})
message("Headers:${headers}")
message("SRC:${src}")

add_executable(
        Test
        Tests.cpp
  )
target_link_libraries(
        Test
        SRC
        gtest_main
)

include(GoogleTest)
add_test(GoogleTest Test)
gtest_discover_tests(Test)

当我运行构建时,它给了我以下错误,我试图在互联网上搜索并没有找到解决方案。

[ 45%] Built target gtest
[ 62%] Built target SRC
[ 70%] Built target gtest_main
[ 79%] Built target gmock
[ 87%] Built target gmock_main
[ 91%] Linking CXX executable Test
CMake Error at /home/fengyuan/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/212.5284.51/bin/cmake/linux/share/cmake-3.20/Modules/GoogleTestAddTests.cmake:77 (message):
  Error running test executable.

    Path: '/home/fengyuan/Desktop/a1835007/2021/s2/oop/practical-08-11/Tests/cmake-build-debug/Test'
    Result: 24
    Output:
      

Call Stack (most recent call first):
  /home/fengyuan/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/212.5284.51/bin/cmake/linux/share/cmake-3.20/Modules/GoogleTestAddTests.cmake:173 (gtest_discover_tests_impl)

如果我删除 target_link_libraries 中的 SRC 库,错误就消失了,我尝试删除此 SRC 库并将文件添加到 add_executables() 部分,但它会产生相同的错误。

测试源代码:

#include <gtest/gtest.h>
#include "Room.h"
#include "RoomFactory.h"
TEST(GetFreeRoomTest_IsOccupied,RoomTest)
{
    //Arrange
    RoomFactory& factory=RoomFactory::getInstance();
    //Act
    Room * room= factory. occupyAFreeRoom(Comfortable);
    //Assert
    EXPECT_FALSE(room->roomStatus==Empty);
}```



 This is making me dying. Pls, help.

【问题讨论】:

    标签: c++ cmake googletest


    【解决方案1】:

    问题已解决,${sources} 中有非项目文件,并包含用于调试的 cmake-build-debug 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多