Skip to content
Advertisement

Why are files not found from parallel folders CMake

To have my .cpp and .h files a little bit sorted up pending on their responsibilitie I decided to put them into seperate folders I used the following structure:

root
|
-CMakeLists.txt [rootCmakeList]
src
|
-main.cpp
…….|
……. math
…….|
…….-CMakeLists.txt[mathCmakeList]
…….-Algebra.h
…….-Algebra.cpp
…….XML[xmlCmakeList]
…….|
…….-CMakeLists.txt
…….-AwesomeXML.h
…….-AwesomeXML.cpp

The [rootCmakeList] looks:

cmake_minimum_required(VERSION 3.11.3)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS_DEBUG  "-g")

project(myProject)
#add exectuable
add_executable(myProject src/main.cpp)

target_include_directories(myProject PUBLIC "${src/XML}") 
target_include_directories(myProject PUBLIC "${src/math}") 

add_subdirectory(src/XML) 
add_subdirectory(src/math) 

target_link_libraries(myProject xml)#needed? 
target_link_libraries(myProject math)#needed? 

The [mathCmakeList] looks:

include_directories(${myProject}src/math)
add_library(math Algebra.cpp) 

The [xmlCmakeList] looks:

include_directories(${myProject}src/xml) 
add_library(xml AwesomeXML.cpp) 

So far so good and no problems. But if I want to #include Algebra.h into AweseomeXML.cpp I can not find the file.

To being honest I am not even sure if the cmake command add_library and target_link_libraries makes really sense here because I do not want to create own libraries of it just want to tidy up a little bit my files pending on their topic.

Advertisement

Answer

myProject doesn’t seem to be a variable you set. Furthermore if you properly set up the library targets, you won’t need to add any of the include directories to myProject manually.

First set up the CMakeLists.txt file for math, since it doesn’t depend on other projects. I recommend moving headers linking libraries use to a subdirectory. I myself usually use include and any path that you want the linking library to use in #includes starts there. Set the include dirs in a way that adds them to the INTERFACE_INCLUDE_DIRECTORIES target property of math.

add_library(math Algebra.cpp
    include/Algebra.h # for IDEs
)

target_include_directories(math # should be static here, since you don't want to deploy the lib by itself
    PUBLIC include # available to both math and linking libs
    PRIVATE .      # only available to math; only necessary, if there are "private" headers
)

Then do the same thing for xml, but since you’re using functionality from math, you need to link it giving you access to its include dirs automatically, since they are available via INTERFACE_INCLUDE_DIRECTORIES:

add_library(xml STATIC
    AwesomeXML.cpp
    include/AwesomeXML.h
)

target_include_directories(xml PUBLIC include)

target_link_libraries(xml PRIVATE math) # change PRIVATE to PUBLIC, if Algebra.h is included in a public header

Now at the toplevel we should make sure that any dependencies of a target are available before the target is defined. Also linking a lib gives you access to their respective public include directories and the public include dirs of dependencies linked publicly:

cmake_minimum_required(VERSION 3.11.3)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS_DEBUG  "-g")

project(myProject)

add_subdirectory(src/math)
add_subdirectory(src/XML) 

#add exectuable
add_executable(myProject src/main.cpp)

target_link_libraries(myProject PRIVATE math xml)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement