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 :