Gen Liu
Gen Liu

Reputation: 655

How to know the gcc optimization level for ndk-build

I was writing some native code on Android using NDK(jni). I want to turn off the gcc compiler optimization. For now I add LOCAL_CFLAGS += -O0 to Android.mk, I'm not sure if it is working.

I wrote some code to test the loop overhead like this:

// gettime
for(int i = 0 ; i<10000;i++)
{

}
// gettime

The time difference is too small that I'm sure that the loop has been deleted by the compiler. I can change i to a volatile variable, but I want to test if I have turned off the compiler optimization correctly.

How can I know the optimization level that is used by gcc(ndk-build), can I set make to verbose to get all the messages?

Thanks in advance.

Upvotes: 4

Views: 4901

Answers (2)

Mārtiņš Možeiko
Mārtiņš Možeiko

Reputation: 12917

See here how to disable optimizations: Android NDK assert.h problems

Basically what you need to do is to add

APP_OPTIM := debug

to your Application.mk file

Upvotes: 8

ams
ams

Reputation: 25559

The compiler defines a macro named __OPTIMIZE__ when optimization is enabled.

If you insert these lines into any C file, then the compile will fail if your make flags didn't work for that file.

#ifdef __OPTIMIZE__
#error Optimization enabled. That's not right!
#endif

Another possibility is to check the arch-specific flags on a built binary file (.o or executable).

readelf -A myfile.o

ARM has a flag that indicates the optimization level, but I think the Android toolchain might be a little old to use that correctly, so YMMV.

Upvotes: 6

Related Questions