Controlling compiler flags in the Android NDK?

I know I can use LOCAL_CFLAGS to pass arguments to the compiler. However, ndk-build is inserting options after my LOCAL_CFLAGS, and therefore they take precedence. For example, I want to specify -Ofast, but ndk-build adds -O2 after my own flags, and since only the last option is the one that has any effect, I’m unable to test my code with certain optimization flags set.

Is there any way to either force my LOCAL_CFLAGS to be the last options on the build command, or to disable ndk-build from using certain flags?

  • View Pager as List View Row Item
  • How to start Activity in adapter?
  • Android OpenGL ES GL10 or GL11
  • RingtonePreference Default value
  • NoSuchMethodError when I use android.widget.RelativeLayout.setBackground
  • My ListView backgrounds go black during scrolling - how to fix?
  • For example, my LOCAL_CFLAGS is set to:

    -Wall -Wno-psabi -Ofast -D CP_USE_DOUBLES=0 -D USE_CHIPMUNK
    

    And the call to g++ that ndk-build makes is:

    /Library/Android/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/arm-linux-androideabi-g++ -MMD -MP -MF ./obj/local/armeabi-v7a/objs/native-activity/Main.o.d -fpic -ffunction-sections -funwind-tables -fstack-protector -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -fno-exceptions -fno-rtti -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -Ijni -Ijni/Chipmunk/include/chipmunk -I/Library/Android/android-ndk-r8b/sources/android/native_app_glue -I/Library/Android/android-ndk-r8b/sources/cxx-stl/stlport/stlport -I/Library/Android/android-ndk-r8b/sources/cxx-stl//gabi++/include -Ijni -DANDROID -Wall -Wno-psabi -Ofast -D CP_USE_DOUBLES=0 -D USE_CHIPMUNK -Wa,--noexecstack -frtti -O2 -DNDEBUG -g -I/Library/Android/android-ndk-r8b/platforms/android-9/arch-arm/usr/include -c jni/Main.cpp -o ./obj/local/armeabi-v7a/objs/native-activity/Main.o

    There’s a lot in there, but specifically notice that it first specifies -Os, then there’s my -Ofast, and then after that there’s -O2. Why it specifies -Os if it’s going to later say -O2, I don’t know, but I’m frustrated my -Ofast is being overridden.

    Related posts:

    Access camera via OpenMAX in Android
    Is there any open source Augmented Reality sdk for android?
    Android DisplayMetrics returns incorrect screen size in pixels on ICS
    How to disable RecyclerView item decoration drawing for the duration of item animations
    Android avd is off-screen
    Compare heap dump (HPROF) files
  • Customizing spinner font
  • How to pop up some part image in android
  • Android Application class being called twice
  • What kind of OCR Java library should I use in Android?
  • How to use private keys from Android storage
  • Gradle. How to generate source code before compilation in android app
  • One Solution collect form web for “Controlling compiler flags in the Android NDK?”

    Add

    APP_CFLAGS += -Ofast
    

    to your Application.mk.

    It will not stop NDK from adding -O2 but it will place your flag after the NDK’s.
    This works for me with NDK r8b.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.