這是我在本網站的第二篇文章,旨在瞭解使用gcc編譯/鏈接過程。當我嘗試創建可執行文件時,需要在鏈接時解析符號,但是當我嘗試創建共享庫時,符號在鏈接時不會解析。當我嘗試使用這個共享庫製作可執行文件時,它們可能會被解決。動手:爲什麼共享庫的符號在鏈接時沒有解決?
bash$ cat printhello.c
#include <stdio.h>
//#include "look.h"
void PrintHello()
{
look();
printf("Hello World\n");
}
bash$ cat printbye.c
#include <stdio.h>
//#include "look.h"
void PrintBye()
{
look();
printf("Bye bye\n");
}
bash$ cat look.h
void look();
bash$ cat look.c
#include <stdio.h>
void look()
{
printf("Looking\n");
}
bash$ gcc printhello.c printbye.c
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/cck21S0u.o: In function `PrintHello':
printhello.c:(.text+0x7): undefined reference to `look'
/tmp/ccNWbCnd.o: In function `PrintBye':
printbye.c:(.text+0x7): undefined reference to `look'
collect2: ld returned 1 exit status
bash$ gcc -Wall -shared -o libgreet printhello.c printbye.c
printhello.c: In function 'PrintHello':
printhello.c:6: warning: implicit declaration of function 'look'
printbye.c: In function 'PrintBye':
printbye.c:5: warning: implicit declaration of function 'look'
所以我的問題是爲什麼當我鏈接共享庫時符號未解決。當我使用這個庫來創建一個可執行文件時,需要完成這項工作(解析下游的符號),但這意味着我們需要知道這個庫在使用這個庫時依賴於什麼,但這不是不可取的嗎?
謝謝, Jagrati
爲什麼這會降低投票率?請注意,在OSX上,您可以使用'-flat_namespace'選項使其像其他操作系統一樣運行。 Linux操作系統。 – Troubadour 2010-07-01 16:19:55