cmake 启用异常C++

agxfikkp  于 2023-06-23  发布在  其他
关注(0)|答案(8)|浏览(269)

我正在尝试为Android制作APP原生代码。原生代码在cplusplus中。每当我尝试执行时,出现以下错误。
H236Plus.cpp:135:错误:禁用异常处理,使用-fexceptions启用
如何使用-fexceptions来启用异常处理,在哪里使用它?

ecr0jaav

ecr0jaav1#

这取决于您使用的运行时。如果您没有使用系统运行时,而是使用ndk-build进行构建,则可以将以下任何内容添加到Android.mk文件中:

  • LOCAL_CPP_FEATURES +=异常(推荐)
  • LOCAL_CPPFLAGS += -f异常

此外,您可以将以下行添加到Application.mk文件中:

  • APP_CPPFLAGS += -f异常

您的NDK文件夹中的docs/CPLUSPLUS-SUPPORT.html中有更多信息

xfb7svmp

xfb7svmp2#

您需要使用CrystaX的custom NDK进行构建。它有完整的libstdc++、RTTI和异常支持。它是我所知道的Android开发的最佳工具。

aij0ehis

aij0ehis3#

-fexception是一个编译器开关。如何使用它取决于您的编译器设置。你用的是什么编译器?构建工具?

wlzqhblo

wlzqhblo4#

在编译器标志中,在Makefile中添加-fexception。

1tu0hz3e

1tu0hz3e5#

如果您使用的是ndk-build构建系统,请参考@Tundebabzy的答案。

对于CMake构建系统

将以下内容添加到模块级build.gradle文件中:

android {
  ...
  defaultConfig {
    ...
    externalNativeBuild {

      // For ndk-build, instead use ndkBuild {}
      cmake {
        // Enables exception-handling support.
        cppFlags "-fexceptions"
      }
    }
  }
}
...

有关更多信息请参阅此链接。

bvn4nwqk

bvn4nwqk6#

在最新版本的Android Studio中,我的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"
        }
    }
}
dsekswqp

dsekswqp7#

只是给任何已经开始使用NDK示例的人的注意。
他们倾向于在CMakeLists.txt中设置-fno-exceptions,您需要删除它:

set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wall -Werror -fno-exceptions -frtti")

您可能需要检查是否需要-Werror

vtwuwzda

vtwuwzda8#

我通过将**cFlags“-fexceptions”**添加到applib文件夹中build.gradle脚本的ndk部分来解决这个问题,如下所示:

ndk {
    ...
    cFlags "-fexceptions"
}

更新:使用较新的Gradle插件,它进入externalNativeBuild/cmake部分如下:

android {
    compileSdkVersion 29

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 29

        externalNativeBuild {
            cmake {
                cppFlags.addAll(["-std=c++11", "-fexceptions", ...])
                ...
            }
        }
    }

...

相关问题