编译一个动态库应用是遇到的奇怪问题
我编写了一个动态库libtest.so。我在写这个动态库应用时遇到一个奇怪的问题。我用gcc -L. -llibtest main.c -o test
时编译器一直提示ld cannot find -llibtest
但是我用如下编译就没有问题,生成的文件也能正常运行
gcc -c main.c
gcc main.o ./libtest.so -o test
不知这是为什么
回复 #1 xhdfzfj 的帖子
gcc -L. -ltest main.c -o test 楼上的,这是什么意思啊回复 #3 xhdfzfj 的帖子
-ltest表示库的名字为libtest.so (或者libtest.a) 按楼上的建议处理,仍然是同样的问题。没有成功啊 演示一把: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
回复 #5 xhdfzfj 的帖子
建议 xhdfzfj 先读一下这个再来问问题。http://catb.org/~esr/faqs/smart-questions.html
页:
[1]