C++ 从命令行调用时将包含目录添加到 CMake

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25849571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:27:15  来源:igfitidea点击:

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

c++cmake

提问by Ayberk ?zgür

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.

我不应该打扰现有的 CMakeLists.txt 文件,但我仍然应该在我的构建中添加一些 g++ 系统包含目录。

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

换句话说,我需要-isystem /path/to/my/include添加到我的编译器标志中,但是当调用类似cmake ...

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

也许像cmake .. -DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS -isystem /path/to/my/include"?有没有办法做到这一点?

采纳答案by avtomaton

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>
    

    ORthe same thing, but environment variable are set only for this CMakecall:

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

    IMPORTANT: you mustclean 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_FLAGSfrom the previous run.

  2. Directly setting CMAKE_CXX_FLAGSin cmake string:

    cmake -DCMAKE_CXX_FLAGS=-isystem\ /path/to/my/include <path to my sources>
    
  1. 在前面的回答沙克拉通过提出一个,即设置与C ++标志的环境变量:

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

    同样的事情,但环境变量仅为此CMake调用设置:

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

    重要提示:在启动任何此表单之前,您必须清理构建目录(即清理 CMake 缓存)。在不清理缓存的情况下,CMake 将继续使用您CMAKE_CXX_FLAGS之前运行的缓存。

  2. 直接CMAKE_CXX_FLAGS在 cmake 字符串中设置:

    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.

我相信它可以通过更“本机”的方式完成,但我没有找到负责 CMake 中标头路径的变量。

回答by sakra

You can set the environment variable CXXFLAGSbefore invoking CMake.

您可以CXXFLAGS在调用 CMake 之前设置环境变量。

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

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

CMake 将CMAKE_CXX_FLAGS使用环境变量中的标志初始化缓存变量。该变量影响所有构建类型。

回答by scrutari

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

-DCMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES=<something>即使没有工具链文件,使用也对我有用。这避免了混乱的编译器标志。

回答by rmbianchi

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

只是对其他答案的补充说明:对于macOS 10.14.5上的CMake 3.15.3,只有使用 CMake 标志的解决方案似乎可以正常工作。

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

所以,就我而言,只有这个解决方案工作正常:

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