Chinaunix首页 | 论坛 | 博客
  • 博客访问: 316757
  • 博文数量: 57
  • 博客积分: 146
  • 博客等级: 入伍新兵
  • 技术积分: 769
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-29 14:57
文章分类
文章存档

2014年(39)

2013年(13)

2012年(5)

我的朋友

分类: LINUX

2013-11-26 14:50:45


文件2.h

点击(此处)折叠或打开

  1. int gohell();

文件2.c

点击(此处)折叠或打开

  1. #include <stdio.h>

  2. int gohell()
  3. {
  4.     printf("go hell!\n");
  5.     return (0);
  6. }

文件1.c

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include "2.h"

  3. int main()
  4. {
  5.     gohell();

  6.     return 0;

  7. }

动态库

点击(此处)折叠或打开

  1. gcc -fPIC -shared -o libtest2.so 2.c

a.out

点击(此处)折叠或打开

  1. gcc 1.c -o a.out /usr/local/src/libtest2.so

运行命令ldd a.out:

点击(此处)折叠或打开

  1. xx@shit:/usr/local/src$ ldd a.out
  2.     linux-gate.so.1 => (0xb77de000)
  3.     /usr/local/src/lib_2.so (给出的是动态库的绝对路径)
这时候你把a.out和libtest2.so拷贝到其他板子上,并把libtest2.so放到/lib目录下,运行a.out的话就会出现一下错误:

点击(此处)折叠或打开

  1. ./a.out: error while loading shared libraries: /usr/local/src/libtest2.so: cannot open shared object file: No such file or directory
因为其他板上的/usr/local/src/目录下没有libtest2.so文件,笨方法就是把libtest2.so放到板子的/usr/local/src目录下。


比较好的方法就是编译a.out,动态库不用绝对路径用相对路径。

点击(此处)折叠或打开

  1. gcc 1.-o a.out -L./ -ltest2
运行命令ldd a.out

点击(此处)折叠或打开

  1. xx@shit:/usr/local/src$ ldd a.out
  2.     linux-gate.so.1 => (0xb7724000)
  3.     libtest2.so => not found
  4.     libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb7557000)
  5.     /lib/ld-linux.so.2 (0xb7725000)
这时你把a.out和libtest2.so拷贝到其他板子上,并把libtest2.so放到/lib目录下,程序运行会自动到/lib目录下找到libtest2.so文件,不会报错。

还有就是通过ldconfig命令,更新ld.conf.cache来让程序在给定目录下查询动态链接库文件。
阅读(803) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~