Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link glibc statically but some other library dynamically with GCC

I need to statically link glibc to my project, because the target platform supports only a very old one ( but it works with statically linked glibc from my toolchain, I have checked it)

Unfortunately, this application has to make use of pthread library, but statically linked libpthread takes too much space.

I would like to statically link glibc, and dynamically pthread.

After running this command

powerpc-unknown-linux-gnu-gcc object_files -lrt -lpthread -Wl,-Bstatic -lc 

I get:

/powerpc-unknown-linux-gnu/bin/ld: cannot find -lgcc_s
like image 248
nkdm Avatar asked Nov 02 '12 00:11

nkdm


People also ask

Is glibc dynamically linked?

Applications built with the glibc toolchain will by dynamically linked by default. Application that load shared libraries at runtime using dlopen() must link with the libdl library ( -ldl ).

Can you link a static library to a dynamic library?

When you want to “link a static library with dynamic library”, you really want to include the symbols defined in the static library as a part of the dynamic library, so that the run-time linker gets the symbols when it is loading the dynamic library.

What is the difference between using dynamically and statically-linked libraries?

What are the differences between static and dynamic libraries? Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.

Which linking memory is saved static or dynamic?

The main difference between static and dynamic linking is that static linking copies all library modules used in the program into the final executable file at the final step of the compilation while, in dynamic linking, the linking occurs at run time when both executable files and libraries are placed in the memory.


2 Answers

There is a -static-libgcc if that may help

like image 113
Anycorn Avatar answered Oct 07 '22 06:10

Anycorn


You should be using -static, not -Wl,-static. The latter bypasses gcc's knowledge, and therefore gcc is still trying to link the shared libgcc_s.so rather than the static libgcc_eh.a.

If your aim is to link libc statically but libpthread dynamically, this is simply not going to work. You cannot mix and match different versions of libpthread; it's part of glibc, just a separate file, and the internals need to match. Even with the same version, I think linking libc statically and libpthread dynamically will be very broken.

If glibc is too big for your needs, you could try an alternate libc like uClibc or musl.

like image 24
R.. GitHub STOP HELPING ICE Avatar answered Oct 07 '22 05:10

R.. GitHub STOP HELPING ICE