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:

JavaScript

The [mathCmakeList] looks:

JavaScript

The [xmlCmakeList] looks:

JavaScript

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.

JavaScript

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:

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement