0

I am trying to create project using C++, GoogleTest and Cmake.

  └── test
        ├── CMakeLists.txt
        ├── main.cpp
        ├── testClass1
        │   ├── CMakeLists.txt
        │   ├── testClass1.cpp
        │   └── testClass1.hpp
        └── testClass2
            ├── CMakeLists.txt
            ├── testClass2.cpp
            └── testClass2.hpp

I have 2 testClasses with several test each built as static libs. CMakeLists.txt :

set( MODULE_NAME testClass1 )

add_library( ${MODULE_NAME}
    testClass1.cpp
    testClass1.hpp
    )

target_link_libraries( ${MODULE_NAME}
    ${GTEST_LIBRARIES}
    libClass1
)

add_test(NAME ${MODULE_NAME} COMMAND ${MODULE_NAME})

My goal is to create one executable file from test/main.cpp, which has libraries libtestClass1.a and libtestClass1.a linked. Is there any way to link them to against main.cpp, so that when i run main executable, all the tests from both libs are also run?

Here is CMakeLists.txt of folder with main.cpp :

set( BIN_NAME mainTest )

add_subdirectory(testClass1)
add_subdirectory(testClass2)

add_executable( ${BIN_NAME}
    main.cpp
)

target_link_libraries( ${BIN_NAME}
    ${GTEST_LIBRARIES}
    testClass1
    testClass2
)

target_include_directories( ${BIN_NAME}
    PUBLIC testClass1
    PUBLIC testClass2
)

And main.cpp :

#include "gtest/gtest.h"
#include "testClass1.h"
#include "testClass2.h"

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
    return ret;
}

Could you please help me correct way of creating CMakeLists.txt files and main.cpp ? Thanks in advance !

UPDATE #1

In the parent directory of the /test dir CMakeLists.txt include those lines to prepare googletest (which is locally located within project) :

add_subdirectory(gtest/googletest)
enable_testing()
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
add_subdirectory( test )
SET( GTEST_LIBRARIES gtest gtest_main gmock gmock_main )

After incorporating changes suggested by @Martin and running cmake and make commands, I end up with with mainTest executable and libs.a in theirs directories. However running executable still runs googletest without any tests actually visible :

console output

Jalu
  • 11
  • 4
  • Are you encountering any error or you just want some advice on how to do things better ? – Martin Sep 05 '22 at 11:47
  • When executing mainTest 0 tests are run. It doesn't see those tests linked as library – Jalu Sep 05 '22 at 12:12
  • Note that `gtest_main` provides standard implementation of `main` function so if you link it then you do not need `main.cpp` which defines own `main()`. – Marek R Sep 05 '22 at 13:53
  • Add the object library `add_library( ${MODULE_NAME} OBJECT testClass1.cpp testClass1.hpp)` – 273K Sep 05 '22 at 14:39
  • 1
    If you want `GTEST_LIBRARIES` variable to be seen in the `test/CMakeLists.txt` and inner `CMakeLists.txt`, then you need to define this variable **before** `add_subdirectory( test )`. – Tsyvarev Sep 05 '22 at 14:56

0 Answers0