如何在cmake文件中添加链接器或编译标志? [英] How to add linker or compile flag in cmake file?
问题描述
我有arm-linux-androideabi-g ++编译器。当我尝试编译简单的hellow世界程序它编译精细。当我测试它通过在代码中添加一个简单的异常处理它也工作(只有通过添加-fexceptions ..我猜它被默认禁用)
这是android设备,我只想使用cmake不ndk-build
例如 - first.cpp
#include< iostream>
using namespace std;
int main()
{
try {
}
catch(...)
{
}
return 0;
}
./ arm-linux-androideadi-g ++ -o先测试。 cpp -fexceptions
它没有问题。
问题。 。我试图通过cmake文件编译文件。
我想添加-fexceptions作为一个标志我试过
set(CMAKE_EXE_LINKER_FLAGS -fexceptions)或set(CMAKE_EXE_LINKER_FLAGSfexceptions)
和
set(CMAKE_C_FLAGSfexceptions)
它仍然显示错误...帮助我... padrone我的无知
解决方案假设您想要添加这些标志:(更好地在常量中声明它们)
SET GCC_COVERAGE_COMPILE_FLAGS-fprofile-arcs -ftest-coverage)
SET(GCC_COVERAGE_LINK_FLAGS-lgcov)
有几种添加方式:
1)最简单的一个(不干净,但简单方便,只适用于编译标志, C ++一次):
add_definitions($ {GCC_COVERAGE_COMPILE_FLAGS})
2)附加到相应的cmake变量
SET(CMAKE_CXX_FLAGS $ {CMAKE_CXX_FLAGS} $ {GCC_COVERAGE_COMPILE_FLAGS})
SET(CMAKE_EXE_LINKER_FLAGS$ {CMAKE_EXE_LINKER_FLAGS} $ {GCC_COVERAGE_LINK_FLAGS})
3)使用目标属性,cf。 doc cmake compile flag target属性,需要知道目标名称
get_target_property(TEMP $ {THE_TARGET} COMPILE_FLAGS)
if(TEMP STREQUALTEMP-NOTFOUND)
SET(TEMP)#设置为空字符串
else()
SET(TEMP$ {TEMP})#一个空白以便与现有内容完全分离
endif $ b#追加我们的值
SET(TEMP$ {TEMP} $ {GCC_COVERAGE_COMPILE_FLAGS})
set_target_properties($ {THE_TARGET} PROPERTIES COMPILE_FLAGS $ {TEMP})
现在我使用方法2。
祝你好运。
i have arm-linux-androideabi-g++ compiler. when i try to compile simple hellow world program it compiles fine. when i test it by adding a simple exception handling in that code it works too (only by adding -fexceptions .. i guess it is disabled by default )
this is for android device and i only want to use cmake not ndk-build
for example - first.cpp
#include <iostream> using namespace std; int main() { try{ } catch(...) { } return 0; }
./arm-linux-androideadi-g++ -o first-test first.cpp -fexceptions
it works with no problem ..
the problem .. i am trying to compile the file by cmake file.
i want to add the -fexceptions as a flag i tried with
set (CMAKE_EXE_LINKER_FLAGS -fexceptions ) or set (CMAKE_EXE_LINKER_FLAGS "fexceptions" )
and
set ( CMAKE_C_FLAGS "fexceptions")
it still show error ... help me .. padrone my ignorance
解决方案Suppose you want to add those flags : (better to declare them in a constant)
SET(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage") SET(GCC_COVERAGE_LINK_FLAGS "-lgcov")
There are several ways to add them :
1) The easiest one (not clean but easy and convenient, works only for compile flags, C & C++ at once) :
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
2) appending to corresponding cmake variables
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" ) SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}" )
3) using target properties, cf. doc cmake compile flag target property and need to know the target name
get_target_property(TEMP ${THE_TARGET} COMPILE_FLAGS) if(TEMP STREQUAL "TEMP-NOTFOUND") SET(TEMP "") # set to empty string else() SET(TEMP "${TEMP} ") # a space to cleanly separate from existing content endif() # append our values SET(TEMP "${TEMP}${GCC_COVERAGE_COMPILE_FLAGS}" ) set_target_properties(${THE_TARGET} PROPERTIES COMPILE_FLAGS ${TEMP} )
Right now I use method 2.
Good luck.
这篇关于如何在cmake文件中添加链接器或编译标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!