这是我在本网站的第二篇文章,旨在了解使用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