Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 879
Gen Liu Avatar asked Dec 10 '22 02:12

Gen Liu


2 Answers

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

like image 109
Mārtiņš Možeiko Avatar answered Jan 06 '23 05:01

Mārtiņš Možeiko


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.

like image 22
ams Avatar answered Jan 06 '23 05:01

ams