Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Solution to Compiling and linking a static C++ library usable from C without linking stdc++



From:   email@hidden
Subject: Solution to Compiling and linking a static C++ library usable from C without linking libstdc++
Date: August 8, 2006 4:51:00 PM EDT
To:   email@hidden

Yesterday I wrote:


I have a program written in C++ and C that exports C functions.  I have been able to create a dynamic library, using:
gcc -dynamiclib ofile1.o ofile2.o -o libfile.dylib -shared-libgcc -lstdc++-static
such that I may simply compile and link a vanilla C program without having to link to the C++ runtime library.  For example:
gcc -O3 my_test.c -o my_test -L../libdir -lfile
NOT
gcc -O3 my_test.c -o my_test -L../libdir -lfile -shared-libgcc -lstdc++-static

The reason I need a static C++ library that can be used by C programs is this: I have a C++ library that will be embedded in the runtime system of a program language compiler that produces assembly code through gcc (not g++).  I cannot change the linker flags the compiler passes to gcc, just for a problem on OS X and I cannot require every developer who uses the system to bundle the dynamic library (libfile.dylib) with every program they ship.

I have also created a static library using libtool:
libtool -static -o libfile.a -s -arch_only ppc *.o
but when I want to link to that library, I have to append -shared-libgcc -lstdc++-static (that is, I have to link to the C++ runtime library) every time.

I cannot create a static library using:
ld -static ...
because ld (or ld called through "gcc -static") produces errors:
(1) if I specify only -fno-weak, I get an error for:
lazy symbol pointers (.non_lazy_symbol_pointer),
(2) if I specify -fno-weak AND -mdynamic-no-pic, I get an error for:
symbol_stubs

Does anyone here have experience with this sort of thing?
Thanks in advance for any help you can give.


I figured out the solution--I guess it would be pretty easy if you simply used "otool -L" on the resulting executable to find the libraries to link to.  Here it is:

Using gcc or ld, prelink the object files into one big object file and then link those to
libstdc++-static.a
libgcc_s.1.dylib OR (in gcc library directory for your system) libgcc_eh.a
libSystem.B.dylib
NOTE: if you pass the flag -shared-libgcc to gcc, -lgcc_s.1 and -lSystem.B are added automatically (-static-libgcc does not work because Apple ships the system libraries without crt0.o--you will get a linker error).

If you link to libgcc_s.1.dylib, you will have to link any executable with both -lgcc_s.1 and -lSystem.B .
If you link to the static library libgcc_eh.a (on my computer it is in the directory /usr/lib/gcc/powerpc-apple-darwin8/4.0.1 ), you will only have to link the executable with -lSystem.B.dylib .

The disadvantages of static linking are:
-your resulting library is huge: almost 3MB larger than it would normally be;
-if your executable links to any other C++ libraries, the stdc++ code is duplicated, not shared

The advantages (and necessity) of static linking are:
-executables (or other libraries) linked to your static C++/C library are stand-alone (you don't have to package programs with that library for user installs)--this was necessary for my purposes because the C++ code was going into a RunTime System (RTS): *every* executable would have to come packaged with an extra dynamic library for something that should be part of the RTS; 
-the linking process is much simpler for autotools (configure, make) builds, since the links to -lSystem.B and -lgcc_s.1 may go anywhere on the command line (you do not have to pass them at the end, as you do with -lstdc++-static).

Pete


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Unix-porting mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/unix-porting/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.