下面是目录结构:

pengdl@localhost:~$ tree test/
test/
├── fun.c
├── Fun.h
└── t1
    └── main.c

1 directory, 3 files
pengdl@localhost:~$

fun.c

#include <stdio.h>

void fun(void)
{
printf("int the fun\n");
}

Fun.h

extern void fun(void);

main.c

#include <stdio.h>
#include <Fun.h> int main(int argc, const char *argv[])
{
printf("in the main\n");
fun(); return ;
}

在test目录下,制作动态库:

pengdl@localhost:~/test$ ls
fun.c  Fun.h  t1
pengdl@localhost:~/test$ gcc -fPIC -c fun.c
pengdl@localhost:~/test$ ls
fun.c  Fun.h  fun.o  t1
pengdl@localhost:~/test$ gcc -shared fun.o -o libfun.so.1
pengdl@localhost:~/test$ ls
fun.c  Fun.h  fun.o  libfun.so.1  t1
pengdl@localhost:~/test$ ln -s /home/pengdl/test/libfun.so.1 libfun.so

注意:软连接必须指向刚才生产的库,否则链接是会出错!!

pengdl@localhost:~/test$ ls
fun.c  Fun.h  fun.o  libfun.so  libfun.so.1  t1
pengdl@localhost:~/test$ mv libfun.so t1/

执行: gcc main.c  -o main -lfun -L ./ -I ../

注:其中 -L ./是在链接是告诉链接器动态库的搜索路径,因为动态库的软连接在当前目录下,所以需要 -L ./ ,否则链接时因为找不到库而出错。-L ./只是告诉链接器在链接时还需要到当前目录下搜索,并不会覆盖其他库的搜索路径。

pengdl@localhost:~/test/t1$ gcc main.c -lfun -I ../ -L ./
pengdl@localhost:~/test/t1$ ./a.out
./a.out: error while loading shared libraries: libfun.so: cannot open shared object file: No such file or directory
pengdl@localhost:~/test/t1$

原因:没有将共享库libfun.so.1拷贝到/uer/lib下,如果拷到/usr/lib下,软连接libfun.so也应该改变。解决方法二:在链接时指定运行时库的搜索路径:gcc main.c -lfun -I ../ -L ./ -Wl,-rpath=./。解决方法三:修改/etc/ld.so.cache中所缓存的动态库路径(如果支持ld.so.cache的话)。这可以通过修改配置文件/etc/ld.so.conf中指定的动态库搜索路径来改变;

Linux动态库的搜索路径的更多相关文章

  1. Linux动态库(.so)搜索路径

    主要内容: 1.Linux动态库.so搜索路径 编译目标代码时指定的动态库搜索路径: 环境变量LD_LIBRARY_PATH指定的动态库搜索路径: 配置文件/etc/ld.so.conf中指定的动态库 ...

  2. LINUX动态库(.SO)搜索路径(目录)设置方法

    LINUX动态库(.SO)搜索路径(目录)设置方法 [root@VM_0_11_centos ld.so.conf.d]# cat /etc/ld.so.confinclude ld.so.conf. ...

  3. linux动态库默认搜索路径设置的三种方法

    众所周知, Linux 动态库的默认搜索路径是 /lib 和 /usr/lib .动态库被创建后,一般都复制到这两个目录中.当程序执行时需要某动态库, 并且该动态库还未加载到内存中,则系统会自动到这两 ...

  4. Linux动态库的查找路径

    前两天写了一个动态库,然后试图编译到程序里面去运行,结果发现编译的时候通过gcc的-L参数来指定路径仅仅能让编译通过,运行时还是会出问题的. 比如下面这个例子: main.c是主程序,sum.c中间含 ...

  5. linux 动态库加载路径修改

    1.在 /etc/ld.so.conf 文件中添加搜索路径,重启或者 ldconfig 生效: 2.在 /etc/ld.so.conf.d 目录下添加 *.conf 文件,其中可以添加搜索路径,重启获 ...

  6. linux动态库加载路径修改

    1.在 /etc/ld.so.conf 文件中添加搜索路径,重启或者 ldconfig 生效: 2.在 /etc/ld.so.conf.d 目录下添加 *.conf 文件,其中可以添加搜索路径,重启获 ...

  7. 【转载】Linux动态库搜索路径的技巧

    转自:http://soft.chinabyte.com/os/232/11488732_2.shtml 众所周知,Linux动 态库的默认搜索路径是/lib和/usr/lib.动态库被创建后,一般都 ...

  8. linux动态库加载时搜索路径

    摘自http://gotowqj.iteye.com/blog/1926613 对动态库的实际应用还不太熟悉的读者可能曾经遇到过类似“error while loading shared librar ...

  9. Linux动态库搜索路径的技巧

    众所周知,Linux动态库的默认搜索路径是/lib和/usr/lib.动态库被创建后,一般都复制到这两个目录中.当程序执行时需要某动态库,并且该动态库还未加载到内存中,则系统会自动到这两个默认搜索路径 ...

随机推荐

  1. 如何处理Android SDK无法更新问题?

    在Android开发中,最痛苦的事情就是相关库升级后,Android SDK太低,跑步起来,最最痛苦的事情就是,想更新,却因大天朝的一墙之隔,愣是没法更新.遇到这种情况怎么办呢?小编和大家分享一个解决 ...

  2. 传递给数据库 'master' 中的日志扫描操作的日志扫描号无效

    错误:连接数据库的时候提示:SQL Server 检测到基于一致性的逻辑 I/O 错误 校验和不正确 C:\Documents and Settings\Administrator>" ...

  3. Python LDAP中的时间戳转换为Linux下时间

    (Get-ADUser zhangsan -Properties badpasswordtime).badpasswordtime返回值为:131172610187388712131172610187 ...

  4. Git – fatal: Unable to create ‘/.git/index.lock’: File exists错误解决办法

    有时候在提交的时候,中间提交出错,导致有文件被lock,所以会报下面的错误: fatal: Unable to create ‘/msg/.git/index.lock’: File exists. ...

  5. Expender Header 与 Content互斥展示

    Expender 在展开时,Header 不展示:不展开时,展示 Header <Expander dxlc:DockLayoutControl.Dock="Top" IsE ...

  6. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  7. mac上使用生成RSA公钥和密钥

    关于RSA加密解密的问题,没事弄了一下,先把主要的流程保存下来,以备交流或者以后用. 首先确保你的电脑上安装了openssl,一般mac系统安装后都会自动安装!怎么安装??.....请百度...... ...

  8. Web service standards: SOAP, REST, OData, and more

    Web service standards: SOAP, REST, OData, and more So far, we've covered the components of a web ser ...

  9. A Simple MVVM Example[Forward]

    In my opinion, if you are using WPF or Silverlight you should be using the MVVM design pattern. It i ...

  10. WPF调用图片路径,或资源图片

    一.加载本项目的图片WPF引入了统一资源标识Uri(Unified Resource Identifier)来标识和访问资源.其中较为常见的情况是用Uri加载图像.Uri表达式的一般形式为:协议+授权 ...