Lorac Lorac - 1 year ago 80
C++ Question

Using an utilities library (headers-only) in multiple modules with cmake

I have this kind of arborescence:

-- Module 1
-- include
-- src
-- Module 2
-- ...
-- utilities (Headers Only - It will have .cpp probably later)
-- include
-- typedefs.h
-- ..

I would like to be able to use my Utilities library into any modules.
I've tried using


My Utilities cmakelist.txt

project(Utilities LANGUAGES CXX)

When I try to include into my cpp files

#include "utilities/include/typedefs.h"

It cannot find the file. I've tried with : (doesn't work)

#include "include/typedefs.h"
#include "typedefs.h"

I'm trying to find what do I need to do into my module CMakeList.txt files so that the compiler can find the #include statement.

The top level has :


I added into the top-level cmakelist.txt : (doesn't work)


I do not have to link it yet, since there is not .cpp file. I just need to be able to #include into the different modules.

Answer Source

You should use target_include_directories along with your target.
As an example:

target_include_directories(my_target PUBLIC "${Utilities_SOURCE_DIR}/include")

Then, all the source files that belong to the target my_target will be able to access to the headers as:

#include <typedefs.h>

Use relative paths otherwise.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download