cmake会自动附加-O2标志

发布于 2025-01-21 06:27:12 字数 2203 浏览 4 评论 0原文

我正在从事一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2025-01-28 06:27:12

https://github.com/android/android/ndk/issues/378 。这就是Cmake的行为方式。较少的特定标志是在更具体的标志之前。您将优化标志添加到通用cmake_cxx_flags变量而不是构建模式特定cmake_cxx_flags_release

cflagscppflags通常行为不佳。用参数中的-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 specific CMAKE_CXX_FLAGS_RELEASE.

cFlags and cppFlags generally don't behave well IME. Either set the appropriate CMake variables in arguments 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文