问题
I am building my app in debug mode, and I notice some errors saying "parent failed to evaluate: no location, value may have been optimized out". Therefore, I try to add "-O0" in my module build.gradle like this:
externalNativeBuild {
cmake {
cppFlags "-O0 -frtti -fexceptions -std=c++11 -DANDROID_ARM_NEON=TRUE -mfloat-abi=softfp "
abiFilters "armeabi-v7a"
}
}
But still, the same error shows up after adding "-O0". May I ask how to disable compiler optimization properly? My android Studio version is 2.3.3, my sdk tool version is 26.0.2 and my ndk version is 15.1.4
回答1:
If you want to disable optimization for release build, you can force Debug for C/C++ only:
android {
defaultConfig {
externalNativeBuild {
cmake {
arguments '-DCMAKE_BUILD_TYPE:STRING=Debug'
回答2:
You can override the build flags by adding the following to your CMakeLists.txt:
# Debug flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
# Release flags
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast")
To verify this has worked check build output in:
app/.externalNativeBuild/cmake/<buildconfig>/<architecture>/build.ninja
Look for a line starting with FLAGS. This doesn't actually replace the existing compiler flags it just appends your flags and these take precedence.
The default flags are inherited from $ANDROID_NDK/build/cmake/android.toolchain.cmake so you could edit that file directly, however, if you update your NDK these changes will be overwritten.
来源:https://stackoverflow.com/questions/45536672/adding-o0-to-cppflags-fails-to-disable-clang-compile-optimization-in-android