Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何缩减编译大小 #835

Closed
shuax opened this issue Jun 11, 2020 · 12 comments
Closed

如何缩减编译大小 #835

shuax opened this issue Jun 11, 2020 · 12 comments
Milestone

Comments

@shuax
Copy link
Contributor

shuax commented Jun 11, 2020

xmake编译动态库出来3500k,用xmake生成的vs2019工程编译出来900k。

感觉是没有开消除引用,手动尝试添加/opt:ref没效果。

可能是自己没用对,但是又不会设置。

@waruqi
Copy link
Member

waruqi commented Jun 11, 2020

设置 mode.minsizerel或者 mode.release模式

add_rules("mode.minsizerel")

target("xxx")
    -- ...

然后切到对应模式来编译

xmake f -m minsizerel
xmake

详情看文档:https://xmake.io/#/zh-cn/manual/custom_rule?id=modeminsizerel

@shuax
Copy link
Contributor Author

shuax commented Jun 11, 2020

解决了,添加/GL以后/OPT:REF可用。

minsizerel编译出来也有3300k。

@waruqi
Copy link
Member

waruqi commented Jun 11, 2020

/OPT:REF文档上说,默认就是启用的,除非被其他flags干扰了,比如 /DEBUG,所以release/minsizeref应该默认就是生效的,而且我这边测试了下,不管是否加 /GL, /OPT:REF 前后大小都一样,没什么变化。。我不知道你这是否设置了啥其他的flags给干扰了

By default, /OPT:REF is enabled by the linker unless /OPT:NOREF or /DEBUG is specified. To override this default and keep unreferenced COMDATs in the program, specify /OPT:NOREF. You can use the /INCLUDE option to override the removal of a specific symbol.

@shuax
Copy link
Contributor Author

shuax commented Jun 11, 2020

我用xmake create创建的工程,稍后我上传一个测试工程。

@shuax
Copy link
Contributor Author

shuax commented Jun 11, 2020

工程在这里:https://github.com/shuax/xmake_test

@waruqi
Copy link
Member

waruqi commented Jun 11, 2020

看了下, /OPT:REF 默认就是开启的,只要单开 /GL就行了,这两其实就是相当于gcc/clang的 -flto,我改进了下 xmake ,现在mode.minsizerel模式默认启用了,配合其他优化flags,你的这个test工程最终生成大小在 749K

你可以执行 xmake update -s dev 更新下脚本试试

@waruqi waruqi added this to the v2.3.5 milestone Jun 11, 2020
@shuax
Copy link
Contributor Author

shuax commented Jun 11, 2020

测试成功

@waruqi
Copy link
Member

waruqi commented Jun 11, 2020

另外你的xmake.lua也可以改进下,有点冗余

target("brotli")
    set_kind("static")
    ---  设置导出,其他add_deps时候,就会自动设置,不用每个target设置一遍
    add_includedirs("brotli/c/include", {public = true})      

    add_files("brotli/c/**.c|tools/*.c")

@waruqi
Copy link
Member

waruqi commented Jun 11, 2020

第三方库,也可以提交到 https://github.com/xmake-io/xmake-repo 然后走 add_requires("brotli") 直接集成进来。

https://xmake.io/#/zh-cn/package/remote_package?id=%e6%b7%bb%e5%8a%a0%e5%8c%85%e5%88%b0%e4%bb%93%e5%ba%93

@shuax
Copy link
Contributor Author

shuax commented Jun 11, 2020

public这个学会了,就是看着比较复杂。

像add_files的|就很帅。

比如

add_includedirs("brotli/c/include|public")   

@waruqi
Copy link
Member

waruqi commented Jun 11, 2020

public这个学会了,就是看着比较复杂。

这还复杂哈,总比你每个target写n编add_includedirs精简,放在{}配置,灵活可扩展,可以扩展其他配置key

像add_files的|就很帅。

这个专门用于排除子目录文件的,对add_includedirs也有效,所以你就不要瞎定义了。。

@waruqi
Copy link
Member

waruqi commented Jun 11, 2020

这里先这样吧

@waruqi waruqi closed this as completed Jun 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants