开发者

removing unnecessary code at compile time

开发者 https://www.devze.com 2023-03-31 05:54 出处:网络
I use the CImg library to write plugins for an image-editing software I created. The problem is that when I include CImg, size of the plugins explodes from 200kb up to 2Mb!

I use the CImg library to write plugins for an image-editing software I created. The problem is that when I include CImg, size of the plugins explodes from 200kb up to 2Mb! But in this particular case I only use 5% of CImg code!

So my question is: is there a way to remove un开发者_如何转开发necessary code at compile time, so the final executable is not bloated?

(I use Qt 4.8.0 and the latest CImg, Qt Creator and MacOS Lion).

Compile/Link flags: QMAKE_CXXFLAGS += -Os -fdata-sections -ffunction-sections LIBS += -Wl --gc-sections


Make sure you are compiling with full optimisations (or just -Os which is size optimisation) and stripping debug symbols with strip -s. That can take up a lot of space.

Also it could be that while you are only using 5% of the CImg code, the 5% you use is using the other 95% internally. Pretty much the best you can do is optimise and strip debug symbols.


The first and most obvious: have you stripped the debugging information? Try the command

strip -s <program>

Also, compiling with -Os may help a bit.


According to gcc documentation, gcc can remove unused non-static functions via dead code elimination when -flto option is combined with either -fuse-linker-plugin or -fwhole-program option.

Clang also supports link time optimization, which can remove unused non-static function across linked files.

0

精彩评论

暂无评论...
验证码 换一张
取 消