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?

  • how take screen shot of currently playing video?
  • Difference between ScrollView and ListView
  • Android how to do image screen slide?
  • Android : References to a Context and memory leaks
  • Android SIP API: Shows supported, yet error 'not supported'
  • Fingerprint API for android phone
  • 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:

    Using clipRect - explanation
    print array in the log cat android
    ListView random IndexOutOfBoundsException on Froyo
    Is there an easy way to add a border to the top and bottom of an Android View?
    Get referrer after installing app from Android Market
    Ringing myself using android emulator
  • Add sdk tools to path in Android Studio app
  • Execution failed for task ':app:packageDebug' - Failed to read zip file
  • MediaController Positioning
  • how to receive text sms to specific port..
  • Run a service with Root privileges or adding permissions with root
  • ViewFlipper vs AdapterViewFlipper
  • One Solution collect form web for “Controlling compiler flags in the Android NDK?”


    APP_CFLAGS += -Ofast

    to your

    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.