传递编译器选项 cmake [英] Passing compiler options cmake

查看:40
本文介绍了传递编译器选项 cmake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用 cmake 命令传递编译器选项

I know how to pass compiler options using the cmake command

set(CMAKE_CXX_FLAGS "-Wall -Wno-dev -Wl,-rpath=/home/abcd/libs/")

还有什么方法可以从命令行传递选项,这将覆盖 CMakeList.txt options ,例如 -

Is there also any way to pass the options from the command line, that will override the CMakeList.txt options , something like -

cmake -Wl,-rpath=/home/abcd/newlibs/ path/to/CMakeLists.txt

cmake -D CMAKE_CXX_FLAGS="-Wno-dev -Wl,-rpath=/home/abcd/libs/" path/to/CMakeLists.txt

我的主要问题是我想知道如何附加标志以及如何从命令行覆盖现有的编译器标志.

My main problem is that I want to know how to append flags and how to override existing compiler flags from the command line.

推荐答案

是的,您可以附加编译器和链接器选项.但是在 CMake 中有两件事你必须区分:首先调用以生成构建环境,以及在更改 CMakeList.txt 文件或依赖项后所有连续调用以重新生成该构建环境.

Yes, you can append compiler and linker options. But there are two things you have to differentiate in CMake: first call to generate the build environment and all consecutive calls for re-generating that build environment after changes to your CMakeList.txt files or dependencies.

这里有一些可能性(不包括更复杂的工具链 变体):

Here are some of the possibilities (excluding the more complex toolchain variants):

  1. 缓存的初始内容CMAKE_CXX_FLAGS 变量是 CMAKE_CXX_FLAGS_INIT 在操作系统/工具链检测期间由 CMake 本身设置,以及在 CXXFLAGS 环境变量中设置的任何内容.所以你最初可以调用:

  1. The initial content from the cached CMAKE_CXX_FLAGS variable is a combination of CMAKE_CXX_FLAGS_INIT set by CMake itself during OS/toolchain detection and whatever is set in the CXXFLAGS environment variable. So you can initially call:

cmake -E env CXXFLAGS="-Wall" cmake ..

  • 后来的 CMake 会期望用户直接修改 CMAKE_CXX_FLAGS 缓存变量以追加内容,例如通过使用像 ccmake 这样的编辑器使用 CMake 提交.

  • Later CMake would expect that the user modifies the CMAKE_CXX_FLAGS cached variable directly to append things e.g. by using an editor like ccmake commit with CMake.

    您可以轻松引入自己的构建类型,例如 ALL_WARNINGS.附加了构建类型特定部分:

    You can easily introduce your own build type like ALL_WARNINGS. The build type specific parts are appended:

     cmake -DCMAKE_CXX_FLAGS_ALL_WARNINGS:STRING="-Wall" -DCMAKE_BUILD_TYPE=ALL_WARNINGS ..
    

  • 附加链接器标志

    链接器选项或多或少等同于编译器选项.只是 CMake 的变量名称取决于目标类型(EXESHAREDMODULE).

    1. CMAKE_EXE_LINKER_FLAGS_INIT, CMAKE_SHARED_LINKER_FLAGS_INITCMAKE_MODULE_LINKER_FLAGS_INIT 与环境变量 LDFLAGS 结合代码>到CMAKE_EXE_LINKER_FLAGSCMAKE_SHARED_LINKER_FLAGSCMAKE_MODULE_LINKER_FLAGS.

    1. The CMAKE_EXE_LINKER_FLAGS_INIT, CMAKE_SHARED_LINKER_FLAGS_INIT or CMAKE_MODULE_LINKER_FLAGS_INIT do combine with the evironment variable LDFLAGS to CMAKE_EXE_LINKER_FLAGS, CMAKE_SHARED_LINKER_FLAGS and CMAKE_MODULE_LINKER_FLAGS.

    所以你可以例如调用:

    cmake -E env LDFLAGS="-rpath=/home/abcd/libs/" cmake ..
    

  • 见上文.

  • See above.

    附加了特定于构建类型的部分:

    Build type specific parts are appended:

    cmake -DCMAKE_SHARED_LINKER_FLAGS_MY_RPATH:STRING="-rpath=/home/abcd/libs/" -DCMAKE_BUILD_TYPE=MY_RPATH ..
    

  • 替代方案

    请注意,CMake 确实提供了特殊变量来以独立于平台的方式设置编译器/链接器标志.所以你不需要知道特定的编译器/链接器选项.

    Alternatives

    Just be aware that CMake does provide special variable to set complier/linker flags in a platform independent way. So you don't need to know the specific compiler/linker option.

    以下是一些示例:

    不幸的是,编译器的警告级别没有(尚未)

    Unfortunately there is none for the compiler's warning level (yet)

    这篇关于传递编译器选项 cmake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆