Linking partially static and partially dynamic in GCC

后端 未结 1 1010

I\'m trying to compile a very simple (as simple as hello world) C program using both dynamic and static linking with GCC. I want to know how to do this in general, so my min

1条回答
  •  爱一瞬间的悲伤
    2021-01-06 10:41

    The following worked for me

    ln -s `gcc -print-file-name=libc.a`
    gcc -static-libgcc -L. -lc test.c
    

    Then ldd a.out gives:

    not a dynamic executable
    

    Edit:

    The OP wants to link one library dynamically and another statically. He have the example of linking libc statically and libm dynamically. That particular case I have not been able to achieve. However, the opposite is possible i.e. linking libc dynamically and libm statically.

    ln -s `gcc -print-file-name=libm.a`
    gcc test.c -L. -lm
    

    then ldd a.out gives

    libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0x41960000)
    /lib/ld-linux.so.2 (0x4193d000)
    

    Note that the link order matters.e.g gcc -L. -lm test.c does not work.

    This works with other libraries as well. For example gomp

    gcc -fopenmp test.c
    

    ldd shows libgomp.so.1. We can link it statically like this

    ln -s `gcc -print-file-name=libgomp.a`
    gcc -L. -fopenmp test.c
    

    Now ldd a.out does not show libgomp.so.1. But in this case pthreads is still linked dynamically. To link pthreads statically requires that libc be linked statically as well.

    0 讨论(0)
提交回复
热议问题