linux找不到动态链接库的解决方案

有时候系统中明明存在某个动态链接库,但是编译运行程序的时候就是发现找不到,而且怎么设置也不起作用,可以试试下面这个终极解决方案。。。。

解决方案:将动态链接库所在的目录添加到 /etc/ld.so.conf 末尾。,执行以下命令 sudo /sbin/ldconfig 使得修改生效。

 

这里总结一下相关知识:

1.头文件搜索路径先后排序:

  1. 当前路径;
  2. 编译时指定的路径,比如--prefix=/usr/local,查找的时候会去/usr/local/include/目录找.h文件;
  3. gcc的specs文件里(参考:GCC 编译配置文件 specs);
  4. 使用-I参数指定的目录(优先级最高);
  5. gcc的环境变量设置 CPATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH OBJC_INCLUDE_PATH  ;
  6. 系统标准的include目录。

GCC looks in several different places for headers. On a normal Unix system, if you do not instruct it otherwise, it will look for headers requested with #include <file> in: /usr/local/include libdir/gcc/target/version/include /usr/target/include /usr/include For C++ programs, it will also look in libdir/../include/c++/version, first. In the above, target is the canonical name of the system GCC was configured to compile code for; often but not always the same as the canonical name of the system it runs on. version is the version of GCC in use.

 

2.动态库的搜索路径先后顺序:

  1. 编译目标代码时指定的动态库搜索路径;
  2. 环境变量LD_LIBRARY_PATH指定的动态库搜索路径;
  3. 配置文件/etc/ld.so.conf中指定的动态库搜索路径;
  4. 默认的动态库搜索路径/lib;
  5. 默认的动态库搜索路径/usr/lib。 

 

3.通过修改系统环境变量设置搜索路径:

  • 使用 gcc 编译时将 头文件 路径添加到 C_INCLUDE_PATH 系统环境变量中;
  • 使用 g++ 编译时将 头文件 路径添加到 CPLUS_INCLUDE_PATH 系统环境变量中;
  • 将 动态连接库 路径添加到 LD_LIBRARY_PATH 系统环境变量中;
  • 将 静态库 路径添加到 LIBRARY_PATH 系统变量中。

 

4.通过添加编译选项设置搜索路径:

  • -I 头文件所在的路径
  • -L 库文件所在的路径(包括静态库和动态库)
  • -l 指定库文件
posted @ 2021-09-25 11:10  禅元天道  阅读(1398)  评论(0编辑  收藏  举报