Android 启用异常C++;
我正在尝试为Android制作应用程序本机代码。 本机代码在cplusplus中。 每当我尝试进行此操作时,都会出现以下错误 H236Plus.cpp:135:错误:异常处理已禁用,请使用-feexceptions启用Android 启用异常C++;,android,c++,makefile,cmake,android-ndk,Android,C++,Makefile,Cmake,Android Ndk,我正在尝试为Android制作应用程序本机代码。 本机代码在cplusplus中。 每当我尝试进行此操作时,都会出现以下错误 H236Plus.cpp:135:错误:异常处理已禁用,请使用-feexceptions启用 如何使用-fexceptions来启用异常处理,以及在哪里使用它?-fexception是一个编译器开关。如何使用它取决于编译器设置。你在用什么编译器?石斑鱼类?编译工具?在编译器标志中,在Makefile中添加-feexception。您需要使用CrystaX的编译工具进行编译
如何使用-fexceptions来启用异常处理,以及在哪里使用它?-fexception是一个编译器开关。如何使用它取决于编译器设置。你在用什么编译器?石斑鱼类?编译工具?在编译器标志中,在Makefile中添加-feexception。您需要使用CrystaX的编译工具进行编译。它完全支持libstdc++、RTTI和异常。它通常是我所知道的Android开发的最佳工具。它取决于您使用的运行时。如果您没有使用系统运行时,而是使用
ndk build
进行构建,则可以将以下任何一项添加到您的Android.mk文件中:
- 本地_CPP_功能+=异常(推荐)
- 本地标志+=-feexceptions
- APP_CPPFLAGS+=-feexceptions
在NDK文件夹的
docs/CPLUSPLUS-SUPPORT.html
中有更多信息我通过在build.gradle脚本的NDK部分添加cFlags“-feexceptions”解决了这个问题,如下所示:
ndk {
...
cFlags "-fexceptions"
}
使用最新版本的Android Studio,my build.gradle的外观如下:
model {
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
buildTypes {
release {
minifyEnabled false
shrinkResources false
proguardFiles.add(file("proguard-rules.txt"))
signingConfig = $("android.signingConfigs.release")
}
}
defaultConfig {
applicationId "my.android.app"
minSdkVersion.apiLevel 16
targetSdkVersion.apiLevel 23
versionCode 29
versionName "my.latest.version"
}
ndk {
moduleName "jni-utils"
ldLibs.add("log")
cppFlags.add("-std=c++11")
cppFlags.add("-fexceptions")
stl "gnustl_static"
}
}
android.signingConfigs {
create("release") {
storeFile "C:\\Android\\Git\\MyProject\\keystore\\keystoreCommon"
storePassword "put you password here"
keyAlias "put your alias here"
keyPassword "put your password here"
}
}
}
如果您使用的是ndk build
build系统,请参考@tundabzy的答案
用于构建系统
将以下内容添加到模块级build.gradle
文件中:
android {
...
defaultConfig {
...
externalNativeBuild {
// For ndk-build, instead use ndkBuild {}
cmake {
// Enables exception-handling support.
cppFlags "-fexceptions"
}
}
}
}
...
有关更多信息请参见。对于刚开始使用NDK示例之一的人,请注意 他们倾向于在CMakeLists.txt中设置
-fno异常
,您需要删除它:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -fno-exceptions -frtti")
您可能需要检查您是否需要
-Werror
。NDK和cygwin。我所做的只是在ndk文件夹的目录中的cygwin中键入“makeapp=project”。这个目录中应该有一个Makefile。这控制了所有内容的编译方式。这是寻找编译器开关的地方。是-fexception还是-fexceptions?这有用吗?这样,您将有-fno异常和-fexceptions。当然您必须删除-fno异常才能使-fexceptions生效吗?要在ndk build中启用它,请在您的Android.mk中使用新的LOCAL_CPP_FEATURES变量,如:LOCAL_CPP_FEATURES+=exceptions