X99
X99

Reputation: 915

removing unnecessary code at compile time

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 unnecessary 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

Upvotes: 2

Views: 1322

Answers (3)

valyala
valyala

Reputation: 17830

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.

Upvotes: 2

Seth Carnegie
Seth Carnegie

Reputation: 75130

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.

Upvotes: 2

rodrigo
rodrigo

Reputation: 98358

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.

Upvotes: 2

Related Questions