我有一个项目,有以下项目结构:项目根目录
--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.
问题解决了,${sources}中有非项目文件,包括用于调试的cmake-build-debug文件。