60

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?

3
  • so you want to add compilation flags and then remove them? Or do you just want to add them? Commented Sep 15, 2014 at 13:57
  • 2
    I just want to add them without touching CMakeLists. Commented Sep 15, 2014 at 14:06
  • Note that the variable reference, $CMAKE_CXX_FLAGS, would reference a shell variable. Probably not what you would want. Actually, it should be useless on the command line. Commented May 28, 2015 at 7:52

5 Answers 5

35

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.

Sign up to request clarification or add additional context in comments.

4 Comments

That IMPORTANT tip saved my sanity!
what if i need multiple directories added to include path from command line?
You can pass more than one -isystem parameter (like using a usual -I way, i. e. -isystem path0 -isystem path1)
Nothing works from this
18

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.

1 Comment

or without export form: CXXFLAGS=-isystem\ /path/to/my/include cmake ..
16

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

2 Comments

I don't think you should override this. It is meant to be set by toolchain files. If you override it you might end up not being able to find standard C++ headers.
This still can be used as a workaround for loading Cmake-based projects into IDEs. Also when LLVM toolchain is used, this option helps force Cmake to see standard headers from LLVM rather than those installed in the system (typically from GNU toolchain of a different version).
6

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>

or

cmake -DCMAKE_C_FLAGS=-I\ /path/to/include <path/to/source>

Comments

1

Another choice: https://cmake.org/cmake/help/latest/command/project.html#code-injection

For example

There has a empty file hdr/myhdr.h

The file hdr/inject.cmake below:

# hdr/inject.cmake
include_directories(hdr)

And the main CMakeLists.txt below:

# CMakeList.txt
cmake_minimum_required(VERSION 3.15)
project(hello CXX)
add_executable(main main.cpp)

The main.cpp below:

#include "myhdr.h"
int main()
{
    return 0;
}

When we using the command cmake . -DCMAKE_PROJECT_INCLUDE=hdr/inject.cmake, we can let include_directories(hdr) to apply on all projects (project(hello) also) in CMakeLists.txt .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.