首页 > 解决方案 > 如何在 ndk 中使用 cmake 设置 LOCAL_LDFLAGS/LOCAL_CPPFLAGS?

问题描述

如何使用 cmake 设置 LOCAL_CPPFLAGS/LOCAL_CFLAGS/LOCAL_LDFLAGS?我想减小这么大,但我读过的教程都是关于 mk 文件的。我应该在 cmakelists.txt 中做什么?

我直接设置了LOCAL_CPPFLAGS/LOCAL_CFLAGS/LOCAL_LDFLAGS,但是好像不行。

set(LOCAL_CPPFLAGS "${LOCAL_CPPFLAGS} -ffunction-sections,-fdata-sections")
set(LOCAL_CFLAGS "${LOCAL_CFLAGS} -ffunction-sections,-fdata-sections")
set(LOCAL_LDFLAGS  "${LOCAL_LDFLAGS} -Wl,--gc-sections,--icf=safe")

标签: gradlecmakeandroid-ndk

解决方案


你应该能够使用这样的东西来做到这一点:

target_compile_options(mytarget PRIVATE -ffunction-sections -fdata-sections)
target_link_libraries(mytarget -Wl,--gc-sections,--icf=safe)

请注意,-ffunction-sections在使用 NDK 的 Clang 时默认启用了一段时间。如果您使用的是 NDK r19c 或更高版本,我相信-fdata-sections默认情况下也会启用。因此,您只需明确指定链接器标志。


推荐阅读