【问题标题】:Enable Exception C++启用异常 C++
【发布时间】:2011-03-14 04:10:25
【问题描述】:

我正在尝试为 Android 制作 APP 原生代码。 本机代码在 cplusplus 中。 每当我尝试制作时,都会出现以下错误。

H236Plus.cpp:135:错误:异常处理已禁用,使用 -fexceptions 启用

如何使用 -fexceptions 来启用异常处理,我在哪里使用它?

【问题讨论】:

  • 要通过 ndk-build 启用它,请在 Android.mk 中使用新的 LOCAL_CPP_FEATURES 变量,如:LOCAL_CPP_FEATURES += exceptions

标签: android c++ makefile cmake android-ndk


【解决方案1】:

这取决于您使用的运行时。如果您没有使用系统运行时并且使用ndk-build 构建,您可以将其中任何一个添加到您的 Android.mk 文件中:

  • LOCAL_CPP_FEATURES += 例外(推荐)
  • LOCAL_CPPFLAGS += -fexceptions

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

  • APP_CPPFLAGS += -fexceptions

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • NDK 与 cygwin。我所做的只是在 ndk 文件夹目录下的 cygwin 中输入“make APP=project”。
      • 这个目录下应该有Makefile。这控制了所有内容的编译方式。那是寻找编译器开关的地方。
      • -fexception还是-fexceptions
      【解决方案4】:

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

      【讨论】:

      • 那有用吗?有了这个,您将拥有 -fno-exceptions 和 -fexceptions。
      • 确定要删除 -fno-exceptions 才能让 -fexceptions 生效吗?
      【解决方案5】:

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

      对于CMake 构建系统

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

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

      更多信息this Link

      【讨论】:

        【解决方案6】:

        使用最新版本的 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"
                }
            }
        }
        

        【讨论】:

          【解决方案7】:

          我通过在 build.gradle 脚本的 ndk 部分添加 cFlags "-fexceptions" 解决了这个问题,如下所示:

          ndk {
              ...
              cFlags "-fexceptions"
          }
          

          【讨论】:

            【解决方案8】:

            对于已经开始使用其中一个 NDK 示例的任何人,请注意。

            他们倾向于在他们的 CMakeLists.txt 中设置-fno-exceptions,您需要将其删除:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-06-17
              • 2012-06-27
              • 2012-08-05
              • 1970-01-01
              • 2011-12-07
              • 1970-01-01
              • 2012-05-29
              • 2015-11-12
              相关资源
              最近更新 更多