Issue
When I add the flag -flto
to my NDK C++ project the linker emits the following error: "Optimization level must be between 0 and 3", even though my optimization level is explicitly set to 3 via -O3.
Does anyone know how to solve this?
The compiler flags are passed via Gradle which, as I understand it, should pass the flags to both the Clang compiler and linker. When I remove the -flto
flag everything works fine.
Notes:
I'm using NDK 19.2 (latest version at the time I write this).
I also get the warning
"clang++.exe: warning: argument unused during compilation: '-Wa,--noexecstack' [-Wunused-command-line-argument]"
which I do not have if I compile without link time optimizations.
Solution
Two parts to the answer:
The error is caused by https://github.com/android-ndk/ndk/issues/721. Clang's LTO plugin just doesn't accept -Os
or -Oz
. This is a bug.
Okay, I might be really stupid, I suppose between 0 and 3 means 1 or 2 :)
It's actually because you can't use the generic cppFlags
to set optimization levels. That corresponds to CMAKE_CXX_FLAGS
, and you need to set these in CMAKE_CXX_FLAGS_DEBUG
and CMAKE_CXX_FLAGS_RELEASE
(and/or the C flavors of those). CMake has its own defaults in those variables and the command line is built as ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE}
, so your -O3
is being overridden by the default.
Answered By - Dan Albert
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.