site  contact  subhomenews

Compiling C++ apps statically

November 05, 2008 — BarryK
Experimenting with Wirbel, I wondered if it could create a static executable. Wirbel creates intermediate C++ code then calls the Gnu C++ compiler, so the final executable requires the libstdc++.so.6 and libgcc_s.so.1 libraries.

The thing is the executable would have to be small, and for plain C programs compiling against Dietlibc or uClibc statically is the way to go. For a C app, you could do this:

# diet gcc -nostdinc -o hello hello.c

A C++ app requires the C shared libraries, but also the C++ shared libraries. One thing at a time, can I compile a C++ app to use the libstdc++.a and libgcc_s.a static libraries? ...er, problem, there is no libgcc_s.a. However, after a bit of research I discovered that this works:

remove /usr/lib/libstdc++.so symlink
# ln -s /usr/lib/gcc/i486-t2-linux-gnu/4.2.2/libgcc_eh.a /usr/lib/libgcc_s.a
remove /usr/lib/libgcc_s.so symlink

The removal of the symlinks forces the linker to fallback and look to see if there is a static library and if so it is used.

After doing the above, I then compile the Wirbel program:
# wic hello.w
# ldd ./hello
linux-gate.so.1 => (0xffffe000)
libm.so.6 => /lib/libm.so.6 (0xb7f33000)
libc.so.6 => /lib/libc.so.6 (0xb7e38000)
/lib/ld-linux.so.2 (0xb7f56000)
...progress!

But, at this stage I don't know how to link in the Dietlibc libraries instead of the standard C libraries.

Comments

Fully static wirbel app
Username: BarryK
Can create a fully static app using standard C libs: # wic -c hello.w -o hello.cc ...this creates C++ source file # wic -v hello.w ...this shows how the c++ compiler is called. # g++ -static -fno-rtti -w -I/usr/include/wirbel -o hello /usr/lib/wirbel/libwirbel.a /usr/lib/wirbel/baustones/fs/file.o hello.cc After stripping, the executable is 447KB. This is mostly because of the C library. The original shared executable was 22KB, when I linked libstdc++ statically it jumped to 89KB.

C++ libs
Username: BarryK
"Puppy has all the C++ shared libs. I'm only trying to compile statically because I want to know if Wirbel could be used to create a static app that will run in the initial ramdisk.


Tags: woof