Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake how to include headers without sources?

This is probably a dummy question but I have literally looked at the two first pages of google without success.
I'm writing a header only library and I'm unable to set up correctly the CMake configuration in order that when I build my solution a given main.cpp finds the proper includes.
How can this be accomplished?

EDIT

So I probably should give a little more detailed explanation.
Lets say I have a ./src folder with: ./src/core and ./src/wrappers. Inside each folder I have .h files that needs to be included in a main.cpp file:

#include <src/core/reader.h>

Still when I put in CMakeList.txt something like:

include_directories(src/core)
add_executable(main main.cpp)

I receive a message like: src/core/reader.h no such file or directory.

like image 450
Jairo Avatar asked Oct 24 '16 17:10

Jairo


People also ask

Do you include header files in CMake?

With CMake, adding header include directories to your C++ project is as easy as using your head in football! Heading those C++ include directories is easy with CMake. As you are probably aware, you can include other source files in C++ with the #include pre-processor directive.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

How do I include a path in CMake?

First, you use include_directories() to tell CMake to add the directory as -I to the compilation command line. Second, you list the headers in your add_executable() or add_library() call.


2 Answers

To be able to use that path, you should refer to the parent directory of src.
Assuming the top level CMakeLists.txt is at the same level of src, you can use this instead:

include_directories(${CMAKE_SOURCE_DIR})

As from the documentation of CMAKE_SOURCE_DIR:

The path to the top level of the source tree.

If src is directly in the top level directory, this should let you use something like:

#include <src/whatever/you/want.h>

That said, a couple of suggestions:

  • I would rather add this:

    include_directories(${CMAKE_SOURCE_DIR}/src)
    

    And use this:

    #include <whatever/you/want.h>
    

    No longer src in your paths and restricted search area.

  • I would probably use target_include_directories instead of include_directories and specify the target to be used for that rules:

    target_include_directories(main ${CMAKE_SOURCE_DIR}/src)
    

    This must be put after the add_executable, otherwise the target is not visible.

like image 178
skypjack Avatar answered Sep 30 '22 07:09

skypjack


Just add a include_directories() directive in order to find where your header only library can be found by the target project.


According to your edit. To find

 #include <src/core/reader.h>

You need to add

 include_directories(/full_parent_path_of_src)
like image 21
πάντα ῥεῖ Avatar answered Sep 30 '22 09:09

πάντα ῥεῖ