Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding include directories to CMake when calling it from the command line

Tags:

c++

cmake

I'm in a situation where I should not disturb the existing CMakeLists.txt files, but I still should add some g++ system include directory to my build.

In other words, I need -isystem /path/to/my/include added to my compiler flags, but when calling something like cmake ...

Maybe something like cmake .. -DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS -isystem /path/to/my/include"? Is there a way to do this?

like image 770
Ayberk Özgür Avatar asked Sep 15 '14 13:09

Ayberk Özgür


People also ask

How do I include a directory in CMake?

To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .

What does Include_directories do in CMake?

Add include directories to the build. Add the given directories to those the compiler uses to search for include files. Relative paths are interpreted as relative to the current source directory.

What are include directories?

The Include Directories corresponds to the environment variable INCLUDE . Directory settings displayed in the window are the directories that Visual Studio will search for include files referred to in your source code files. Corresponds to environment variable INCLUDE.

How do I change my path in CMake?

CMake will use whatever path the running CMake executable is in. Furthermore, it may get confused if you switch paths between runs without clearing the cache. So what you have to do is simply instead of running cmake <path_to_src> from the command line, run ~/usr/cmake-path/bin/cmake <path_to_src> .


4 Answers

I have the same problem. I found two solutions:

  1. The one proposed by sakra in a previous answer, i.e. setting an environment variable with C++ flags:

    export CXXFLAGS=-isystem\ /path/to/my/include
    cmake <path to my sources>
    

    OR the same thing, but environment variable are set only for this CMake call:

    CXXFLAGS=-isystem\ /path/to/my/include cmake <path to my sources>
    

    IMPORTANT: you must clean your build directory (i.e. clean the CMake cache) before launching any of this form. Without cleaning the cache, CMake will continue using your cached CMAKE_CXX_FLAGS from the previous run.

  2. Directly setting CMAKE_CXX_FLAGS in cmake string:

    cmake -DCMAKE_CXX_FLAGS=-isystem\ /path/to/my/include <path to my sources>
    

I believe that it can be done by a more 'native' way, but I didn't find a variable responsible for paths to headers in CMake.

like image 96
avtomaton Avatar answered Oct 04 '22 11:10

avtomaton


You can set the environment variable CXXFLAGS before invoking CMake.

$ export CXXFLAGS=-isystem\ /path/to/my/include
$ cmake ..

CMake will the initialize the cache variable CMAKE_CXX_FLAGS with the flags from the environment variable. The variable affects all build types.

like image 33
sakra Avatar answered Oct 04 '22 11:10

sakra


Using -DCMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES=<something> worked for me even without toolchain file. This avoids cluttering compiler flags.

like image 32
scrutari Avatar answered Oct 04 '22 11:10

scrutari


Just an additional note to the other answers: with CMake 3.15.3 on macOS 10.14.5, only the solution using the CMake flag seems to work properly.

So, in my case, only this solution worked fine:

cmake -DCMAKE_CXX_FLAGS=-I\ /path/to/include <path/to/source>
like image 21
rmbianchi Avatar answered Oct 04 '22 11:10

rmbianchi