https://www.cnblogs.com/MYSQLZOUQI/p/5901765.html
moonx@moonx:mygcc$ ls
libmytest.a main my_so_test.h st1.o st2.o test_a.o test_b.o test_c.o
libtest.so main.c st1.c st2.c test_a.c test_b.c test_c.c
moonx@moonx:mygcc$ cat test_*.c
#include "my_so_test.h"
void testA()
{
printf("In AAA
");
}
#include "my_so_test.h"
void testB()
{
printf("In BBB
");
}
#include "my_so_test.h"
void testC()
{
printf("In CCC
");
}
moonx@moonx:mygcc$ cat test_
test_a.c test_a.o test_b.c test_b.o test_c.c test_c.o
moonx@moonx:mygcc$ cat my_so_test.h
#include <stdio.h>
void testA();
void testB();
void testC();
moonx@moonx:mygcc$ gcc -shared -fPIC -o libtest.so test_*.o
moonx@moonx:mygcc$ gcc main.c -L . -ltest -o main
moonx@moonx:mygcc$ ldd main
linux-vdso.so.1 => (0x00007ffe6d7fc000)
libtest.so => /usr/download/test/mygcc/libtest.so (0x00007fac1c26c000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fac1bea2000)
/lib64/ld-linux-x86-64.so.2 (0x00007fac1c46e000)
moonx@moonx:mygcc$
偶忍不住又要罗嗦一句了,相信俺,我的唠叨对大家是有好处。我为什么用这种方法呢?
因为我是在给大家演示动态库的用法,完了之后我就把libtest.so给删了,然后再重构ld.so.cache,对我的系统不会任何影响。
倘若我是开发一款软件,或者给自己的系统DIY一个非常有用的功能模块,那么我更倾向于将libtest.so拷贝到/lib、/usr/lib目录下,
或者我还有可能在/usr/local/lib/目录下新建一文件夹xxx,将so库拷贝到那儿去,并在/etc/ld.so.conf.d/目录下新建一文件mytest.conf,
内容只有一行“/usr/local/lib/xxx/libtest.so”,再执行ldconfig。
如果你之前还是不明白怎么解决那个“not found”的问题,那么现在总该明白了吧。
巨坑:设置了/etc/ld.so.conf,运行了ldconfig,编译仍找不到动态库
https://bbs.csdn.net/topics/390042184
你理解错了,ld.so是动态库载入器而不是连接器,某些翻译“动态连接器”有问题的,或者说英文原文就有问题
换句话说,配置ld.so.conf的作用就是如果so在非标准路径,可执行文件能够找到,这是运行期做的事情
你现在是编译,ld.so根本不起作用,所有用非标准路径的话必需手动指定库目录