Freddyy
Freddyy

Reputation: 69

CMake gtest 'Error running test executable' Result:24

I have a project and have the following project structure: -project-root

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

My 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)

When I run the build, it gives me the following error, I tried to search on the internet and haven't found solutions yet.

[ 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)

The error has gone if I remove the SRC library in the target_link_libraries, I tried to remove this SRC lib and add the files in the add_executables() section but it produce the same error.

Test Source Code:

#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.

Upvotes: 2

Views: 6548

Answers (1)

Freddyy
Freddyy

Reputation: 69

Problem solved, there are non-project files in ${sources} and included the cmake-build-debug files used for debugging.

Upvotes: 2

Related Questions