如何在 CMake 文件中添加链接器或编译标志?
问题描述
我正在使用 arm-linux-androideabi-g++
编译器.当我尝试编译一个简单的Hello, World!"时程序它编译得很好.当我通过在该代码中添加一个简单的异常处理来测试它时,它也可以工作(在添加 -fexceptions
之后......我猜它默认是禁用的).
I am using the arm-linux-androideabi-g++
compiler. When I try to compile a simple "Hello, World!" program it compiles fine. When I test it by adding a simple exception handling in that code it works too (after adding -fexceptions
.. I guess it is disabled by default).
这是针对Android设备的,我只想使用CMake,而不是ndk-build
.
This is for an Android device, and I only want to use CMake, not ndk-build
.
例如 - first.cpp
./arm-linux-androideadi-g++ -o first-test first.cpp -fexceptions
它没有问题...
问题 ...我正在尝试使用 CMake 文件编译该文件.
The problem ... I am trying to compile the file with a CMake file.
我想添加 -fexceptions
作为标志.我试过
I want to add the -fexceptions
as a flag. I tried with
和
它仍然显示错误.
推荐答案
注意:考虑到 CMake 的演变,自从这个答案被写出来,这里的大部分建议现在已经过时/不推荐使用,并且有更好的替代方案
Note: Given CMake evolution since this was answer was written, most of the suggestions here are now outdated/deprecated and have better alternatives
假设您想添加这些标志(最好在常量中声明它们):
Suppose you want to add those flags (better to declare them in a constant):
有几种方法可以添加它们:
There are several ways to add them:
最简单的一个(不干净,但简单方便,并且只对编译标志、C 和 C++ 一次有效):
The easiest one (not clean, but easy and convenient, and works only for compile flags, C & C++ at once):
附加到相应的 CMake 变量:
Appending to corresponding CMake variables:
使用目标属性,参见.doc CMake 编译标志目标属性 并且需要知道目标名称.
Using target properties, cf. doc CMake compile flag target property and need to know the target name.
现在我使用方法 2.
这篇关于如何在 CMake 文件中添加链接器或编译标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!