xhdfzfj 发表于 2009-11-05 17:55

编译一个动态库应用是遇到的奇怪问题

我编写了一个动态库libtest.so。我在写这个动态库应用时遇到一个奇怪的问题。我用
gcc -L. -llibtest main.c -o test
时编译器一直提示ld cannot find -llibtest
但是我用如下编译就没有问题,生成的文件也能正常运行
gcc -c main.c
gcc main.o ./libtest.so -o test

不知这是为什么

jzhang918 发表于 2009-11-05 22:34

回复 #1 xhdfzfj 的帖子

gcc -L. -ltest main.c -o test

xhdfzfj 发表于 2009-11-06 09:28

楼上的,这是什么意思啊

EricFisher 发表于 2009-11-06 09:53

回复 #3 xhdfzfj 的帖子

-ltest表示库的名字为libtest.so (或者libtest.a)

xhdfzfj 发表于 2009-11-08 11:03

按楼上的建议处理,仍然是同样的问题。没有成功啊

cjaizss 发表于 2009-11-08 22:07

演示一把:

linux-0gt0:/tmp/ch/b # cat test.c
int func(int i)
{
      return i+1;
}
linux-0gt0:/tmp/ch/b # cat main.c
#include <stdio.h>
int func(int);
int main()
{
      printf("%d\n",func(1));
      return 0;
}
linux-0gt0:/tmp/ch/b # gcc -shared -Wl,-soname,libtest.so -o libtest.so test.c
linux-0gt0:/tmp/ch/b # gcc -c main.c
linux-0gt0:/tmp/ch/b # gcc main.o -L. -ltest -o test
linux-0gt0:/tmp/ch/b # ./test
./test: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
linux-0gt0:/tmp/ch/b # mv libtest.so /lib
linux-0gt0:/tmp/ch/b # ./test
2

jzhang918 发表于 2009-11-09 23:23

回复 #5 xhdfzfj 的帖子

建议 xhdfzfj 先读一下这个再来问问题。

http://catb.org/~esr/faqs/smart-questions.html
页: [1]
查看完整版本: 编译一个动态库应用是遇到的奇怪问题