Reputation: 1256
I have a working project and I want to add google test into it, but one difference is, my main()
function is in a different file.
This is my project structure:
-test
- include
- file.h
- file1.h
- file2.h
- file.cpp
- file1.cpp
- file2.cpp
- main.cpp
- CMakeLists.txt
- unitTest
-CMakeLists.txt
-test_main.cpp
My main CMakeLists.txt
file links a lot of libs and files that the test_main.cpp
will also need, but it also creates an executable file. I tried using this question but I can't figure out how to make it work or even if it's possible.
This is my unitTest/CMakeList.txt
:
cmake_minimum_required(VERSION 3.14)
# Download and unpack googletest at configure time
project(Google_tests)
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
# Now simply link against gtest or gtest_main as needed. Eg
include(${CMAKE_CURRENT_SOURCE_DIR}/../CMakeLists.txt)
add_executable(Google_tests test_main.cpp)
target_link_libraries(Google_tests gtest gtest_main)
add_test(NAME example_test COMMAND Google_tests)
This CMakeList file is from google test git hub, and I also tried to do what the Clion Manual wrote, but it's still not working. This is the error that I'm getting:
-- Configuring done
CMake Error at /home/yaodav/Desktop/dnr_main_repo/test/CMakeLists.txt:74 (add_executable):
Cannot find source file:
main.cpp
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx
Call Stack (most recent call first):
CMakeLists.txt:30 (include)
CMake Error at /home/yaodav/Desktop/dnr_main_repo/test/CMakeLists.txt:74 (add_executable):
No SOURCES given to target: test
Call Stack (most recent call first):
CMakeLists.txt:30 (include)
CMake Generate step failed. Build files cannot be regenerated correctly.
Here is the test/CMakeList.txt:
cmake_minimum_required(VERSION 3.14)
project(test)
set(CMAKE_CXX_STANDARD 17)
if (CMAKE_BUILD_TYPE MATCHES Release)
add_definitions(-DRELEASE=1)
add_definitions(-DDEBUG=0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
else()
add_definitions(-DRELEASE=0)
add_definitions(-DDEBUG=1)
endif ()
include_directories(/include)
link_directories(list of dir)
#link_libraries(../lib/external/tinyxml2-master)
find_package(Threads) # for enableing std::thread
find_library(conn_LIB connlib)
SET(SOURCE_FILES main.cpp
file.cpp file.h
file1.cpp file1.h
file2.cpp file2.h )
add_executable(test ${SOURCE_FILES})
SET(LBS list of libs)
target_link_libraries(test ${list of libs} pq)
Upvotes: 3
Views: 2094
Reputation: 18243
The following line in your unitTest/CMakeLists.txt
file is leading to behavior you may not expect:
include(${CMAKE_CURRENT_SOURCE_DIR}/../CMakeLists.txt)
From the documentation for include
, this command will:
Load and run CMake code from a file or module.
This command is typically reserved for loading CMake Find Modules or other custom CMake modules (i.e. *.cmake
files). As such, this does not change the current CMake source directory, so files mentioned in test/CMakeLists.txt
are still interpreted with respect to the child directory test/unitTest
. After calling include()
, CMake looks in test/unitTest
for the main.cpp
file (and others) but it does not exist there. Hence, CMake prints the error.
Although not recommended, if you really need to traverse to the parent CMakeLists.txt file (using include()
) like this, you can use CMAKE_CURRENT_LIST_DIR
to refer to the source files:
SET(SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/main.cpp
${CMAKE_CURRENT_LIST_DIR}/file.cpp
${CMAKE_CURRENT_LIST_DIR}/include/file.h
${CMAKE_CURRENT_LIST_DIR}/file1.cpp
${CMAKE_CURRENT_LIST_DIR}/include/file1.h
${CMAKE_CURRENT_LIST_DIR}/file2.cpp
${CMAKE_CURRENT_LIST_DIR}/include/file2.h
)
add_executable(test ${SOURCE_FILES})
You likely need to make similar changes to other relative references in the test/CMakeLists.txt
file, to prevent errors resulting from this abnormal approach.
My recommendation is to instead run CMake from your top-level CMakeLists.txt file, and simply use the following line to traverse to the CMake file in the sub-directory:
add_subdirectory(unitTest)
Upvotes: 3