Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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.

like image 972
Cornstalks Avatar asked Nov 10 '12 16:11

Cornstalks


1 Answers

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.

like image 172
Andrey Kamaev Avatar answered Sep 28 '22 01:09

Andrey Kamaev