I have the following directory structure:
main.cpp
CMakeLists.txt
src/
some_function.h
some_function.cpp
some_class.h
some_class.cpp
CMakeLists.txt
test/
catch.hpp
tests.cpp
CMakeLists.txt
CmakeLists.txt in the project root:
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 11) # C++11
project(main)
add_subdirectory (src)
add_executable(main main.cpp)
target_link_libraries (main some_class)
CmakeLists.txt in the src/:
add_library (some_class some_class.h some_class.cpp some_function.h some_function.cpp)
The above works to build and run the main target.
Now I want to build and run tests. The file tests.cpp includes some_function.h and some_class.h. However, I am not sure how to add the src/ directory here.
This is what I have so far in test/ (results in a linking error for the function in some_function.h):
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 11) # C++11
project(tests)
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
add_executable(tests tests.cpp)
target_link_libraries(tests Catch)