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).
This is for an 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 with a 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 displays an error.
In newer versions of CMake you can set compiler and linker flags for a single target with
target_compile_options
andtarget_link_libraries
respectively (yes, the latter sets linker options too):The advantage of this method is that you can control propagation of options to other targets that depend on this one via
PUBLIC
andPRIVATE
.As of CMake 3.13 you can also use
target_link_options
to add linker options which makes the intent more clear.You can also add linker flags to a specific target using the
LINK_FLAGS
property:If you want to propagate this change to other targets, you can create a dummy target to link to.
Checkout the ucm_add_flags and ucm_add_linker_flags macros of ucm (my set of useful CMake macros) - they deal with appending compiler/linker flags.
Suppose you want to add those flags (better to declare them in a constant):
There are several ways to add them:
The easiest one (not clean, but easy and convenient, and works only for compile flags, C & C++ at once):
Appending to corresponding CMake variables:
Using target properties, cf. doc CMake compile flag target property and need to know the target name.
Right now I use method 2.
Try setting the variable
CMAKE_CXX_FLAGS
instead ofCMAKE_C_FLAGS
:The variable
CMAKE_C_FLAGS
only affects the C compiler, but you are compiling C++ code.Adding the flag to
CMAKE_EXE_LINKER_FLAGS
is redundant.