2010-07-10 51 views
19

我想爲Android製作APP本機代碼。本地代碼在cplusplus中。 每當我嘗試製作時,都會出現以下錯誤。啓用異常C++

H236Plus.cpp:135: error: exception handling disabled, use -fexceptions to enable

如何使用-fexceptions啓用異常處理,以及在哪裏使用它?

+0

要使用ndk-build啓用它,請在您的Android.mk中使用新的LOCAL_CPP_FEATURES變量,如下所示: LOCAL_CPP_FEATURES + =例外 – hB0 2013-11-07 20:37:12

回答

5

您需要使用CrystaX的custom NDK構建。它具有完整的libstdC++,RTTI和異常支持。它通常是我知道的Android開發的最佳工具。

4

-fexception是一個編譯器開關。你如何使用它取決於你的編譯器設置。你使用什麼編譯器? IDE?構建工具?

+0

使用cygwin的NDK。我所做的只是在cygwin的ndk文件夾目錄中輸入「make APP = project」。 – Gidiyo 2010-07-10 15:07:01

+0

這個目錄應該有一個Makefile。這控制着如何編譯所有東西。這是尋找編譯器開關的地方。 – EricSchaefer 2010-07-10 18:05:45

+0

它是** - fexception **還是** - fexceptions **? – 2017-05-23 19:05:53

3

在編譯器標記中在您的Makefile中添加-fexception。

+0

曾經有過嗎?有了這個,你將有-fno-exceptions和-fexceptions。 – ognian 2010-07-12 08:19:15

+0

當然,您必須刪除-fno-exceptions以使-fexceptions生效? – marienke 2013-02-14 13:44:18

26

這取決於您使用的是什麼運行時。如果你不使用的系統運行,並與ndk-build正在建設中,添加任何這些你的Android.mk文件:

  • LOCAL_CPP_FEATURES + =異常(推薦)
  • LOCAL_CPPFLAGS + = -fexceptions

此外,您可以將下面的行添加到您的Application.mk文件:

  • APP_CPPFLAGS + = -fexceptions

在NDK文件夾

1

我加入CFLAGS 「-fexceptions」到的build.gradle腳本NDK部分,這樣解決了這個問題,還有更精彩的信息docs/CPLUSPLUS-SUPPORT.html

ndk { 
    ... 
    cFlags "-fexceptions" 
} 
2

與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" 
     } 
    } 
} 
0

請參閱this如果您使用ndk-build構建系統,則由@Tundebabzy回答。

對於CMake構建系統

以下內容添加到您的模塊級build.gradle文件:

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

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

欲瞭解更多信息,請參閱this Link