Suggestion: add sections to default CXXFLAGS

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Suggestion: add sections to default CXXFLAGS

Post by hendu »

For all GCC-based builds, you might want to add "-ffunction-sections -fdata-sections" to the cxxflags for all lib builds.

No difference for the shared lib, for the static lib this means that the app that uses it can more efficiently drop unused parts of the irrlicht static library. In one of my apps, linking the sectioned libIrrlicht.a vs the old one, dropped binary size some 300kb.

(Users: your app needs -Wl,-gc-sections in LDFLAGS for the linker to do this. But I'm sure you all already had this enabled ;)
Since I was now rebuilding irr anyway, had to check whether it had these flags. It didn't.)


tl;dr smaller binary if the static irrlicht is built with these flags.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Suggestion: add sections to default CXXFLAGS

Post by hybrid »

Sorry, but this seems to be a very special situation where you get smaller binaries. At least that's what the gcc manual says:
Only use these options when there are significant benefits from doing so. When you specify these options, the assembler and linker will create larger object and executable files and will also be slower. You will not be able to use gprof on all systems if you specify this option and you may have problems with debugging if you specify both this option and -g.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Suggestion: add sections to default CXXFLAGS

Post by hendu »

Note that refers to the speed of the assembler and linker, not the final app. The bigger .o files are of no concern, and the bigger executable is only valid when the -gc-sections flag is not passed to the linker.

It helps with binary size in practically all cases; even with binaries of only few files, there's bound to be an unused variable or function somewhere. I have yet to meet a build where it increased size (with the linker option, of course), and I've been using these for years.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Suggestion: add sections to default CXXFLAGS

Post by hybrid »

Ok, well then it seems that gcc documentation is improvable ;-) I found another site which suggested to use -Os as well, even though this won't enable the separate sections part. BTW: Does this reduction also hold for methods, or just global scope C functions?
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Suggestion: add sections to default CXXFLAGS

Post by hendu »

Now that I don't know, but I'd assume methods are counted as functions and so can be individually pruned.
Post Reply