2017-01-28 12 views
1

gcc 링커의 작동 방식과 공유 라이브러리를 다른 공유 라이브러리에 링크 할 때와 공유 라이브러리와 바이너리를 연결할 때 상황이 어떻게 다른지 이해하는 데 어려움을 겪고 있습니다. 우분투 16.04.1 LTS, gcc 5.4.0 및 ld 2.26.1을 사용하고 있습니다.gcc 링커가 심볼을 해석합니다.

다음은 일부 C 소스 파일에서 실행되는 일련의 명령 세트입니다.

순서 1 : -

[email protected]:~/linktest6$ cat a.c 
#include <stdio.h> 

int a_func() { 
    printf("Running a_func()\n"); 

    return 0; 
} 

[email protected]:~/linktest6$ cat b.c 
#include <stdio.h> 

int a_func(); 

int b_func() { 
    printf("Running b_func()\n"); 
    a_func(); 

    return 0; 
} 

[email protected]:~/linktest6$ cat d.c 
#include <stdio.h> 

int b_func(); 

int d_func() { 
    printf("Running d_func()\n"); 
    b_func(); 

    return 0; 
} 

[email protected]:~/linktest6$ cat myprog.c 
#include <stdio.h> 

int d_func(); 

int main() { 
    printf("Running myprog_func()\n"); 
    d_func(); 

    return 0; 
} 

[email protected]:~/linktest6$ gcc -shared -fPIC -o liba.so a.c 

[email protected]:~/linktest6$ ldd -r liba.so 
    linux-vdso.so.1 => (0x00007ffc6fded000) 
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1898da3000) 
    /lib64/ld-linux-x86-64.so.2 (0x0000555e2853c000) 

[email protected]:~/linktest6$ gcc -shared -fPIC -o libb.so b.c 

[email protected]:~/linktest6$ ldd -r libb.so 
    linux-vdso.so.1 => (0x00007ffd82127000) 
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0cc6253000) 
    /lib64/ld-linux-x86-64.so.2 (0x00005586e6a36000) 
undefined symbol: a_func (./libb.so) 

[email protected]:~/linktest6$ gcc -shared -fPIC -o libd.so d.c 

[email protected]:~/linktest6$ ldd -r libd.so 
    linux-vdso.so.1 => (0x00007ffc3addb000) 
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f408db59000) 
    /lib64/ld-linux-x86-64.so.2 (0x0000558720efc000) 
undefined symbol: b_func (./libd.so) 

[email protected]:~/linktest6$ gcc -fPIC -o myprog myprog.c -L. -ld -lb -la 

[email protected]:~/linktest6$ ldd -r myprog 
    linux-vdso.so.1 => (0x00007ffe807aa000) 
    libd.so => not found 
    libb.so => not found 
    liba.so => not found 
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f344acce000) 
    /lib64/ld-linux-x86-64.so.2 (0x0000563a89eb4000) 
undefined symbol: d_func (./myprog) 

순서 2 : - 나는 성공적으로 첫 번째 경우가 아닌 두 번째의 바이너리를 컴파일 할 수 있어요 왜

[email protected]:~/linktest6$ cat a.c 
#include <stdio.h> 

int a_func() { 
    printf("Running a_func()\n"); 

    return 0; 
} 

[email protected]:~/linktest6$ cat b.c 
#include <stdio.h> 

int a_func(); 

int b_func() { 
    printf("Running b_func()\n"); 
    a_func(); 

    return 0; 
} 

[email protected]:~/linktest6$ cat d.c 
#include <stdio.h> 

int b_func(); 

int d_func() { 
    printf("Running d_func()\n"); 
    b_func(); 

    return 0; 
} 

[email protected]:~/linktest6$ cat myprog.c 
#include <stdio.h> 

int d_func(); 

int main() { 
    printf("Running myprog_func()\n"); 
    d_func(); 

    return 0; 
} 

[email protected]:~/linktest6$ gcc -shared -fPIC -o liba.so a.c 

[email protected]:~/linktest6$ ldd -r liba.so 
    linux-vdso.so.1 => (0x00007ffe8c9ee000) 
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f88b91c2000) 
    /lib64/ld-linux-x86-64.so.2 (0x0000555cf72ef000) 

[email protected]:~/linktest6$ gcc -shared -fPIC -o libb.so b.c 

[email protected]:~/linktest6$ ldd -r libb.so 
    linux-vdso.so.1 => (0x00007fffbffc4000) 
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f69ea310000) 
    /lib64/ld-linux-x86-64.so.2 (0x000055996b3e5000) 
undefined symbol: a_func (./libb.so) 

[email protected]:~/linktest6$ gcc -shared -fPIC -o libd.so d.c -L. -lb -Wl,-rpath=/home/ammal/linktest6 

[email protected]:~/linktest6$ ldd -r libd.so 
    linux-vdso.so.1 => (0x00007ffe21bb6000) 
    libb.so => /home/ammal/linktest6/libb.so (0x00007fca1cdb6000) 
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fca1c9d5000) 
    /lib64/ld-linux-x86-64.so.2 (0x000055f517b11000) 
undefined symbol: a_func (/home/ammal/linktest6/libb.so) 

[email protected]:~/linktest6$ gcc -fPIC -o myprog myprog.c -L. -ld -lb -la 
./libb.so: undefined reference to `a_func' 
collect2: error: ld returned 1 exit status 

내 질문이다.

답변

0

시퀀스 2에서 libd.so는 공유 객체 (즉, libb.so)에 연결됩니다. gcc는 그것을 해결할 경로가 필요합니다. 다음을 시도하십시오. $ gcc -fPIC -o myprog myprog.c -L. -ld -lb -la -Wl, -rpath =/home/ammal/linktest6