How to link with dynamic lib (.so) and static libc.a

I am trying to link to static libc.a and dynamic lib.so unsuccessfully.

I have already tried the following:

  • First, I test all dynamic:

    • gcc -shared libtest.c -o libtest.so
    • gcc -c main.c -o main.o
    • gcc main.o -o test -L. -ltest

    Works (compiles and runs)

  • Secondly, I am testing what I want (dynamic lib and static libc):

    • gcc -shared libtest.c -o libtest.so
    • gcc -c main.c -o main.o
    • gcc main.o -o test libtest.so/usr/lib/libc.a

    This is a compilation, but when executed it is segfault! Sagittarius shows that he is trying to access libc.so !!!

  • Finally, I tried to compile a simple program without a link to a dynamic lib

    • gcc -static main.c -> compile ok, run ok
    • gcc main.c / usr / lib / libc.a → compile ok, run: segmentation fault (strace show that it has access to libc.so)

How to do it?

thanks

+6
gcc linker shared-libraries static-libraries
source share
1 answer

The second test (the one you want to do) works for me on i686-linux:

$ cat libtest.c #include <stdio.h> void foo() { printf("%d\n", 42); } $ cat main.c #include <stdio.h> extern void foo(); int main() { puts("The answer is:"); foo(); } $ export LD_LIBRARY_PATH=$PWD $ gcc -shared libtest.c -o libtest.so && gcc -c main.c -o main.o && gcc main.o -o test -L. -ltest && ./test The answer is: 42 $ gcc -shared libtest.c -o libtest.so && gcc -c main.c -o main.o && gcc main.o -o test libtest.so /usr/lib/libc.a && ./test The answer is: 42 

However, you should understand that the shared library you created depends on the shared libc. Thus, it is natural that he tries to open it at runtime.

 $ ldd ./libtest.so linux-gate.so.1 => (0xb80c7000) libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb7f4f000) /lib/ld-linux.so.2 (0xb80c8000) 

One way to achieve what you want to use is: -static-libgcc -Wl,-Bstatic -lc .

+6
source share

All Articles