Code::Blocks, linker and libraries

I'm new to C++ and I'm even newer to all that other stuff like compiler, linker, libraries etc.. I just switched from Microsoft Visual C++ 2008 Express Edition to Code::Blocks + GNU GCC Complier. =]
The problem start when I created new executable. It was 457 kB "fat"! O.o I copy -> paste source code to the old compiler - the size was less than 10 Kb. =] I tried to change compiler... I gain -20% of that size. Not enough. =[
Then I asked my friend for help and he said that it's something about linkers/libraries etc.. I go to settings of C::B, but can't find anything useful.

I'm really new to it - as u can guess - so help me, please!^^
I use code::blocks as well. I looked through my options to see if there was anything I can suggest.

Notes:
I switched to the VS2008 compiler on Code::Blocks, my test.cpp came to about 120kb, with a 90kb .obj file.

On my custom build g++ compiler with Code::Blocks I got 1080kb with a 2kb .o file.
Directly compiling with the g++ gave me the same size. Enabling the -s flag on g++ gave me a size of 514kb.

I have no linked library either, so I'm guessing its just the size of the header for GCC and the code it puts into the binary file versus Windows which might be using different methods to compile a code for its system.

I may boot into my linux machine and get a result there with GCC.

For those curious I ran this code for a test. Run time was about 0.011 seconds average.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Test
#include <iostream>

using namespace std;
int main()
{
    double d = 0.0;
    for (int i = 0; i < 10; i++)
    {
      d += .4;
      cout << d << endl;
    }

    return 0;
}

Interesting results. Same code on Linux Ubuntu with GCC.

Same code as above, executable size = 7.5kb
GCC is native to Linux and UNIX systems.
Depends on whether you statically or dynamically link to the standard library.
Edit:
On Ubuntu 10.10 64bit with g++ 4.5.2:
Dynamically linked: 7.8kB
Statically linked: 4.6MB
Last edited on
Interesting Bazzy, Can you tell me how you change such a setting with GCC? Also does such a feature work with MinGW?
http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#Link-Options
-static
On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.

-shared-libgcc
-static-libgcc
On systems that provide libgcc as a shared library, these options force the use of either the shared or static version respectively. If no shared version of libgcc was built when the compiler was configured, these options have no effect.

There are several situations in which an application should use the shared libgcc instead of the static version. The most common of these is when the application wishes to throw and catch exceptions across different shared libraries. In that case, each of the libraries as well as the application itself should use the shared libgcc.

Therefore, the G++ and GCJ drivers automatically add -shared-libgcc whenever you build a shared library or a main executable, because C++ and Java programs typically use exceptions, so this is the right thing to do.

If, instead, you use the GCC driver to create shared libraries, you may find that they will not always be linked with the shared libgcc. If GCC finds, at its configuration time, that you have a non-GNU linker or a GNU linker that does not support option --eh-frame-hdr, it will link the shared version of libgcc into shared libraries by default. Otherwise, it will take advantage of the linker and optimize away the linking with the shared version of libgcc, linking with the static version of libgcc by default. This allows exceptions to propagate through such shared libraries, without incurring relocation costs at library load time.

However, if a library or main executable is supposed to throw or catch exceptions, you must link it using the G++ or GCJ driver, as appropriate for the languages used in the program, or using the option -shared-libgcc, such that it is linked with the shared libgcc.

-static-libstdc++
When the g++ program is used to link a C++ program, it will normally automatically link against libstdc++. If libstdc++ is available as a shared library, and the -static option is not used, then this will link against the shared version of libstdc++. That is normally fine. However, it is sometimes useful to freeze the version of libstdc++ used by the program without going all the way to a fully static link. The -static-libstdc++ option directs the g++ driver to link libstdc++ statically, without necessarily linking other libraries statically.
I use the default to link dynamically:

g++ bad03.cpp (size 7547 kb on my sample code).

To link statically,

g++ -static bad03.cpp (532223 kb).


Just out of interest, the dynamic one links to the following at runtime:

ldd ./a.out
linux-gate.so.1 => (0xffffe000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x4003c000)
libm.so.6 => /lib/tls/libm.so.6 (0x40119000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x4013f000)
libc.so.6 => /lib/tls/libc.so.6 (0x4014a000)
/lib/ld-linux.so.2 (0x40000000)

The static one, of course, doesn't link to anything.


Last edited on
Topic archived. No new replies allowed.