Hello,
I am trying to figure out how to include files into my project, that are outside of the project directory. The files structure on the system looks the following:
Then I would suggest adding the repo as a submodule (or whatever your VCS calls nested repositories) and keep the CMake code “above” where the sources live.
In that case I would need to keep a repo inside of a repo on purpose with gitignore- thats possible, but I am wondering- doesnt cmake support the way of manually providing the directory that is outside of the tree?
CMake has notions about what is “in the source tree” and not based on the relationship to the top-level CMakeLists.txt in use for the build. These assumptions are in many places, so doing this is likely to trip over odd behaviors. You can make it work, but it isn’t as easy.
You can certainly give absolute paths to the sources to add to targets in the projects directory, but I suspect you’ll always get things like absolute paths to source files rather than any relative paths (for example).
You can do it with one CMakeLists.txt; that’s fine. There’s no nesting requirements within a project either as all non-IMPORTEDadd_{library,executable} targets are global in scope.
Not really because I don’t have your source code . But something like this (based on your image):
cmake_minimum_required(VERSION 3.16) # pick whatever version has the features you want
project(MyProject)
add_library(Submodule1
MyLib/Submodule1/foo.c
MyLib/Submodule1/foo.c)
add_library(Submodule2
MyLib/Submodule2/foo.c
MyLib/Submodule2/foo.c)
add_library(MyLib
MyLib/foo.c
MyLib/foo.h)
target_link_libraries(MyLib PRIVATE # Choose the visibility as needed
Submodule1 Submodule2)
That should at least start a build going. You’ll probably need boilerplate for testing, installation, flags, options, etc. The tutorial has examples to follow.
Thanks for that! But just a question- in your example, do you assume that the Submodule1 and Submodule1 will be compiled as separate shared libraries? Or is it just an intermediate step and it is the target_link_libraries line which creates the output? Or in other words, is this:
add_library(Submodule1
MyLib/Submodule1/foo.c
MyLib/Submodule1/foo.h)
add_library(Submodule2
MyLib/Submodule2/foo.c
MyLib/Submodule2/foo.h)
add_library(MyLib
MyLib/foo.c
MyLib/foo.h)
target_link_libraries(MyLib PRIVATE # Choose the visibility as needed
Submodule1 Submodule2)
Those are two separate effects. One makes 2 libraries and an executable (which links to them). The other makes a single executable. I would recommend going through the Tutorial.
By “an executable (which links to them)” did you mean a shared library, or actual executable? Because from what I understand, in the end both scripts produce a shared lib?
I have already started the tutorial- no doubt I need it.