Here is an MWE that isolates a problem I have in a larger project.
(I am still wrapping my head around CMake
.)
Here is the layout of the project:
Inside tests/CMakeLists.txt
we have
cmake_minimum_required(VERSION 3.13)
add_executable(test_class_a test_class_a.cpp)
target_include_directories(test_class_a PUBLIC ${CMAKE_SOURCE_DIR}/modules/class_a/)
Now, when I try to compile test_class_a
, it complains with something like
Essentially, although inside
test_class_a.cpp
file I have included #include "class_a.hpp"
and it looks fine there, somehow class_a.cpp
does not get invited into the party, it seems.
How I make sure it does, without having to add #include "class_a.cpp"
, which would be ugly?
Inside the top-most CMakeLists.txt
I have of course add_subdirectory(modules/tests/)
.
EDIT: the comment below says I need to add it inside add_executables
, and it does fix the problem. However, this can be my misconception, but shouldn't it be the case that class_a.hpp
and class_a.cpp
are somehow semantically "linked" together, without me having to include the *.cpp inside the sources explicitly? Sorry for a long question, you are certainly free to delete it.
EDIT:
Based on the accepted answer below, I've settled on the following CMakeLists.txt
, which from now on will likely be my modus operandi in CMake
:
cmake_minimum_required(VERSION 3.13)
add_library(class_a ${CMAKE_SOURCE_DIR}/modules/class_a/class_a.cpp)
add_executable(test_class_a test_class_a.cpp)
target_include_directories(test_class_a PUBLIC ${CMAKE_SOURCE_DIR}/modules/class_a/)
target_link_libraries(test_class_a PUBLIC class_a)