如何在linux下写动态链接库并卖给别人?
2015-07-24 23:50
627 查看
前面我们讨论了如何玩Windows下的静态链接库、动态链接库, 也说了linux下的静态链接库, 现在是该说说linux下的动态链接库了。 在本文中, 我们不讲动态链接库有哪些好处, 你去网上搜, 一搜一大堆。 我们仅仅来说说, 如何在linux下写动态链接库, 如何卖掉。
卖方
步骤一:
写test.h文件:
步骤二:
制作动态链接库, 如下:
步骤三:
以1块钱的价格, 吧libtest.so和test.h卖给别人。
买方
步骤一:
写应用程序main.c, 如下:
步骤二:
花1块钱, 从卖方那里买来libtest.so和test.h, 并使用它们, 如下:
OK, linux下的动态链接库先说到这里, 后面还会继续侃, 先睡觉。
卖方
步骤一:
写test.h文件:
void print();写test.c文件:
#include <stdio.h> #include "test.h" void print() { printf("I am a little bit hungry now.\n"); }
步骤二:
制作动态链接库, 如下:
[taoge@localhost learn_c]$ ls test.c test.h [taoge@localhost learn_c]$ gcc -c test.c [taoge@localhost learn_c]$ ls test.c test.h test.o [taoge@localhost learn_c]$ gcc -shared -fPCI -o libtest.so test.o [taoge@localhost learn_c]$ ls libtest.so test.c test.h test.o [taoge@localhost learn_c]$ file libtest.so libtest.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (GNU/Linux), dynamically linked, not stripped [taoge@localhost learn_c]$
步骤三:
以1块钱的价格, 吧libtest.so和test.h卖给别人。
买方
步骤一:
写应用程序main.c, 如下:
#include "test.h" int main() { print(); return 0; }
步骤二:
花1块钱, 从卖方那里买来libtest.so和test.h, 并使用它们, 如下:
[taoge@localhost learn_c]$ ls libtest.so main.c test.h [taoge@localhost learn_c]$ gcc main.c -L. -ltest [taoge@localhost learn_c]$ ./a.out ./a.out: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory [taoge@localhost learn_c]$ cp libtest.so /usr/lib cp: cannot create regular file `/usr/libtest.so': Permission denied [taoge@localhost learn_c]$ su root Password: [root@localhost learn_c]# cp libtest.so /usr/lib [root@localhost learn_c]# ./a.out I am a little bit hungry now. [root@localhost learn_c]# exit exit [taoge@localhost learn_c]$ ./a.out I am a little bit hungry now. [taoge@localhost learn_c]$ ls a.out libtest.so main.c test.h [taoge@localhost learn_c]$ rm libtest.so test.h main.c [taoge@localhost learn_c]$ ls a.out [taoge@localhost learn_c]$ ./a.out I am a little bit hungry now. [taoge@localhost learn_c]$可见, 要把动态链接库放到/usr/lib下, 才能被加载, 而要在这个目录下添加文件, 必须有root权限。 其余的, 不说自明吧。
OK, linux下的动态链接库先说到这里, 后面还会继续侃, 先睡觉。
相关文章推荐
- Linux bridge-utils tunctl 使用
- Linux bridge-utils tunctl 使用 分类: kvm Linux 2015-07-24 23:50 149人阅读 评论(0) 收藏
- linux rsync远程同步(续)
- Linux下软件安装
- Linux下进程的内存结构
- 基于Linux的智能家居的设计(3)
- Linux文件夹共享(NFS)
- Linux Rootkit之三:系统调用劫持简介
- 基于Linux的智能家居的设计(2)
- linux at91看门狗驱动设置
- centos6.6下安装GreenPlum4.3.5.2
- 基于Linux的智能家居的设计(1)
- linux配置java环境变量(详细)
- 3.1 linux进程 2015/7/22
- dd if=/dev/zero of=的含义是什么?Linux 下的dd命令使用详解
- linux 之makefile语法学习1
- Linux SSH 免密码登录
- Linux 多学习过程
- Linux内核学习方法
- Linux系统调用