g++ help

Started by
8 comments, last by Rattrap 18 years, 9 months ago
I tried compiling a little bit of code in g++ under cygwin. MSVC 2003, produces a file about 24kb. g++ produces a file around 465kb. I'm figuring the difference is because of compiler flags, which I am very ignorant of the gcc/g++ ones. Does anyone know some quick flags for gcc/g++ that should help? Right now I'm just doing: g++ -03 main.cpp which is successfully compiling. [Edited by - Rattrap on July 22, 2005 2:46:48 PM]

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Advertisement
It's nothing to do with compiler flags, it's to do with static vs dynamic C++ libraries.

I am assuming that you are using the C++ standard library (if you're not, it should make little difference).

If you are, then g++ will normally statically link some of it.

MSVC *may* use a DLL for its C++ library - thus reducing the binary size.

Ensure that you are comparing a stripped binary (i.e. one without debugging info).

Using -O3 is not generally recommended - more than 1 -O apparently may result in code that doesn't necessarily do quite what you expect (although I've never experienced this).

In any case, the difference between -O and -O3 is negligible in most cases.

---

Note that in gcc, you can enable debugging and optimisation if you want but this does not normally make a lot of sense, as optimised binaries can't be debugged meaningfully because stuff that you're trying to go through in the debugger may be optimised out.

Mark
Try the -Os flaag for the 'fastest/smallest' optimization. Also, which version of gcc are you using? Earlier versions might not have as good an optimizer.

Maybe your file is so big because you have symbolic debug info turned on? I'm not sure what the default is, but if it's on then the program size should be much bigger.

As far as I know MSVC links wth the static standard library by default, though using the DLL is an option.
I'm not trying to do debug, I was just seeing if the code would compile under another compiler. The MSVC 2003 generated file was generated in release mode. From what I can tell, it is staticly linking to the Single-Threaded C++ standard library. I also tried the multithreaded C++ library, the file size went up about 12k, but it is still no where close to the 400+kb executable that g++ is making.

Also the g++ version is 3.4.4

[edit]
Also, if I switch to DLL linking in MSVC, the file drops to about 3kb.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Try the strip -program (strip ). I'm not too sure what it actually does, but it seems to work :)
try -fomit-frame-pointer and strip your executable (IE: $ strip test.exe )
Quote:Original post by Anonymous Poster
... I'm not too sure what it actually does, but it seems to work :)


It strips the executable from debug information (symbols).
I tried the -g option (enabling debug), and the file got about 100k bigger, so it looks like debug is probably disabled.

I tried the strip program, and it cut the file size in about half (down to around 250kb).

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

There are ways to further shrink your program... strip is a big one, there's some compiler flags you can use as well. However, a good bit of that extra chunk is library code that's been linked statically rather than dynamically. The good thing is that this increase should not be seen as a percentage increase, but rather an absolute increase (probably along with a percentage increase or DEcrease, which I don't know)
I tried removing #include <iostream> from my program (I wasn't actually using anything in there at the moment, I put it in by force of habit). The g++ compiled app dropped to about 8kb (pre stripping). 2kb after. What it looks like is happening is, g++ is adding a bunch of code from the standard library that isn't being used at all, or at least its not removing unused code.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

This topic is closed to new replies.

Advertisement