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)