cmake会自动附加-O2标志
我正在从事一个Android Native项目。我在build.gradle
externalNativeBuild {
cmake {
arguments "--warn-uninitialized",
"-DANDROID_TOOLCHAIN=clang",
"-DANDROID_STL=c++_shared",
"-DANDROID_PLATFORM=android-21",
"-DAPP_ALLOW_MISSING_DEPS=true",
"-DSHARED_LIBRARY=1",
"-DOFFLINE_SYNC_SUPPORT_ENABLED=0",
"-DT5_TARGET_PLATFORM=Android",
"-DVERSION_STANDARD=14",
"-DREQUIRED_STANDARD=OFF",
"-DCMAKE_BUILD_TYPE=Release"
cFlags "-Werror=return-type", "-Wno-pragmas", "-DBOOST_THREAD_PROVIDES_FUTURE",
"-DBOOST_THREAD_PROVIDES_FUTURE_CONTINUATION",
"-DJPEG_DISABLE_OPTIM -D_FILE_OFFSET_BITS=32",
"-mno-unaligned-access",
"-Wno-extern-c-compat"
cppFlags "-Os", "-Wno-conversion-null", "-fexceptions", "-frtti", "-std=c++14", "-DES_ENABLE_LOGGING"
}
构建后,我检查build.ninja
文件后,我在build.gradle
中都有此配置-O2
:
build CMakeFiles/myapp.dir/native-lib.cpp.o: CXX_COMPILER__myapp_Release /Users/vmangal/AndroidStudioProjects/MyApplication2/app/src/main/cpp/native-lib.cpp || cmake_object_order_depends_target_myapp
DEFINES = -Dmyapp_EXPORTS
DEP_FILE = CMakeFiles/myapp.dir/native-lib.cpp.o.d
FLAGS = -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -Os -Wno-conversion-null -fexceptions -frtti -std=c++14 -DES_ENABLE_LOGGING -O2 -DNDEBUG -fPIC
OBJECT_DIR = CMakeFiles/myapp.dir
OBJECT_FILE_DIR = CMakeFiles/myapp.dir
可以在标志中看到-O2
标志的标志:
-Os -Wno-conversion-null -fexceptions -frtti -std=c++14 -DES_ENABLE_LOGGING -O2 -DNDEBUG -fPIC
实际上,我想降低lib的大小。 -os
选项似乎被-O2
选项覆盖。有没有办法告诉cmake
不要附加此类标志。
I am working on an android native project. I have this configuration in my build.gradle
externalNativeBuild {
cmake {
arguments "--warn-uninitialized",
"-DANDROID_TOOLCHAIN=clang",
"-DANDROID_STL=c++_shared",
"-DANDROID_PLATFORM=android-21",
"-DAPP_ALLOW_MISSING_DEPS=true",
"-DSHARED_LIBRARY=1",
"-DOFFLINE_SYNC_SUPPORT_ENABLED=0",
"-DT5_TARGET_PLATFORM=Android",
"-DVERSION_STANDARD=14",
"-DREQUIRED_STANDARD=OFF",
"-DCMAKE_BUILD_TYPE=Release"
cFlags "-Werror=return-type", "-Wno-pragmas", "-DBOOST_THREAD_PROVIDES_FUTURE",
"-DBOOST_THREAD_PROVIDES_FUTURE_CONTINUATION",
"-DJPEG_DISABLE_OPTIM -D_FILE_OFFSET_BITS=32",
"-mno-unaligned-access",
"-Wno-extern-c-compat"
cppFlags "-Os", "-Wno-conversion-null", "-fexceptions", "-frtti", "-std=c++14", "-DES_ENABLE_LOGGING"
}
After build when I check build.ninja
file, I see that CMake
is appending some flags automatically including -O2
:
build CMakeFiles/myapp.dir/native-lib.cpp.o: CXX_COMPILER__myapp_Release /Users/vmangal/AndroidStudioProjects/MyApplication2/app/src/main/cpp/native-lib.cpp || cmake_object_order_depends_target_myapp
DEFINES = -Dmyapp_EXPORTS
DEP_FILE = CMakeFiles/myapp.dir/native-lib.cpp.o.d
FLAGS = -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -Os -Wno-conversion-null -fexceptions -frtti -std=c++14 -DES_ENABLE_LOGGING -O2 -DNDEBUG -fPIC
OBJECT_DIR = CMakeFiles/myapp.dir
OBJECT_FILE_DIR = CMakeFiles/myapp.dir
As it can be seen in flags, -O2
flag is appended:
-Os -Wno-conversion-null -fexceptions -frtti -std=c++14 -DES_ENABLE_LOGGING -O2 -DNDEBUG -fPIC
Actually , I want to decrease size of lib. -Os
option seems to be overridden by -O2
option. Is there a way to tell CMake
not to append such flags.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
https://github.com/android/android/ndk/issues/378 。这就是Cmake的行为方式。较少的特定标志是在更具体的标志之前。您将优化标志添加到通用
cmake_cxx_flags
变量而不是构建模式特定cmake_cxx_flags_release
。cflags
和cppflags
通常行为不佳。用参数
中的-dcmake_cxx_flags_release
(或任何构建模式)设置适当的cmake变量 -dcmake_cxx_flags_release 。选项2是更好的IMO,因为它可以将C ++构建配置保持在一个地方。https://github.com/android/ndk/issues/378. This is how CMake behaves. Less specific flags come before more specific flags. You're adding your optimization flags to the generic
CMAKE_CXX_FLAGS
variable instead of the build mode specificCMAKE_CXX_FLAGS_RELEASE
.cFlags
andcppFlags
generally don't behave well IME. Either set the appropriate CMake variables inarguments
with-DCMAKE_CXX_FLAGS_RELEASE
(or whatever build mode), or set them in your CMakeLists.txt. Option 2 is better IMO because it keeps your C++ build config in one place.