static linking of C++ libraries with gcc/g++

D

daniel_shaw01

I want to link the C++ standard libraries statically for my application
because I don't space for the .so files on my target platform.

After quite a bit of searching, I found very little information that
was helpful. The best information I found was at this link
http://fresco.org/~njs/c++-without-stdc++

I followed the approach here and used -nostdlib to disable the default
linking. Instead I use the options:

-lsupc++ -lgcc -lgcc_eh -lc -lgcc -lgcc_eh

where -lgcc -lgcc_eh has to be repeated twice for some reason.

In summary the linker options I used are

-Wl,-Bstatic -lsupc++ -lgcc -lgcc_eh -lc -lgcc -lgcc_eh

followed by -Wl,-Bdynamic to link dynamically with some other
libraries. The problem I found is this:

warning: cannot find entry symbol _start; defaulting to 000877e0

Can anyone suggest why this might be occurring? OR give me some
pointers to some useful information on the topic of static link for
C++?

Thanks,
DS.
 
M

Mike Hewson

I want to link the C++ standard libraries statically

I thought they were! If static here means "does not change while the
program is executing", as opposed to dynamic ( Microsoft dll's et al ).
 
D

Dietmar Kuehl

I want to link the C++ standard libraries statically for my application
because I don't space for the .so files on my target platform.

That's an interesting assessment: you don't have space for the shared
object but you expect to have space for the application with the
moral equivalent being linked in? I'd consider this to be relatively
unlikely: this only works if you don't use major parts of the standard
library (neither explicitly nor implicitly by other stuff you use).
After quite a bit of searching, I found very little information that
was helpful. The best information I found was at this link
http://fresco.org/~njs/c++-without-stdc++

I followed the approach here and used -nostdlib to disable the default
linking. Instead I use the options:

-lsupc++ -lgcc -lgcc_eh -lc -lgcc -lgcc_eh

Of course, this whole gcc stuff is environment specific and off-topic
in comp.lang.c++ and probably also in most of the other forums...
However: why don't you use the option "-static"? This links the
standard
library statically as long as you have a static version thereof. Use of
-nostdlib is more intended to people using a different implementation
of
the standard C++ library and is typically a little bit more involved
than just that...
warning: cannot find entry symbol _start; defaulting to 000877e0

I think this symbol and a few others you'll need are defined in crt1.o,
crti.o, and crtn.o. Why these symbols are not included when specifying
the option -nostdlib, I don't know.
 
G

GTO

I am reading your post via comp.lang.c++.

You forgot to add gnu.gcc.help to your cross-posting. The right choice of
group is half the answer. The audience of gnu.gcc.help should be of more
help.

BTW, buy yourself a copy of Kurt Wall and William von Hagen's The Definitive
Guide to GCC, Apress, 2004.

Gregor

PS: Did you try -static?
 
Joined
May 3, 2008
Messages
1
Reaction score
0
C++ without stdc++

AFIK:

g++ includes stdc++ library by default as well as all the other C++ libraries that are required and the "base" gcc libraries etc to build the program.

If you turn off the stdlibs with -nostdlib you need to know exactly what all the reqiured libs that g++ was implicitly including to get a c++ program to compile.

As these libraries seem to be split into camps: gcc related ones and c++ related ones the problem of finding all the included required libraries to compile with g++ -nostdlib can be halved: use gcc to link and then just find out which c++ libraries are required!

E.g

g++ -I. -c main.cpp

to compile the program to object using g++, then

gcc main.o -lsupc++

to obtain the executable. As you can see the only c++ required apart from stdc++ seems to be libsupc++. LDD analysis of the created program seems to indicate that this library is statically linked in.


Cheers,

ANdy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top