Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn on compiler optimization for Android Studio debug build via Cmake

I am using Android Studio 3.0 for my NDK based app. For the C++ code, I use CMake as the external builder.

This works well, I can create debug and release binaries.

However, I would like to turn on compiler optimizations (say -O3) for a part of the C++ code (the physics engine), not just for the release build, but also for the debug build.

So create the bulk of the debug build as is, without optimizing, yet, I want one of the static library targets to be built with the compiler optimization enabled.

How can I go about this?

I have a CMakeLists for a static library target that gets included using add_subdirectory() directive in the top level CMakeLists file.

Note that I point to the top level CMakeLists in my app's build.gradle file like this:

externalNativeBuild {
    cmake {
        path '../../Android/jni/CMakeLists.txt'
    }
}
like image 262
Bram Avatar asked Oct 26 '25 16:10

Bram


1 Answers

It turns out that you can use the target_compile_options() macro in your CMakeLists.txt with a config specification like this:

target_compile_options(opende PRIVATE
"$<$<CONFIG:RELEASE>:-O3>"
"$<$<CONFIG:DEBUG>:-O3>"
)

This macro adds to the existing compile options.

like image 162
Bram Avatar answered Oct 28 '25 08:10

Bram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!