码迷,mamicode.com
首页 > 其他好文 > 详细

GCC生成动态库

时间:2016-06-13 06:34:29      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

以下内容仅作为本人工作笔记

main.c

1 #include <stdio.h>
2 void hello(void);
3 int main(int argc, char ** argv) {
4         printf("This is main function!\n");
5         hello();
6         return 0;
7 }

hello.c

1 #include <stdio.h>
2 void hello() {
3         printf("This is hello function!\n");
4 }

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1:gcc -shared -fPIC -o libmyhello.so hello.c

把hello.c生成动态库。

-shared表示共享,用作动态库。

-fPIC position independent code表示位置无关代码,用于动态加载。

 

2:gcc -o myhello main.c -L. -lmyhello

-l‘xxxx‘ 表示提取libxxxx.so库文件。如实例则表示libmyhello.so。

-L‘path‘ 表示库文件的位置在path目录下。如实例则表示在当前目录 . 。

 

3:sudo cp libmyhello.so /usr/lib/

把动态库复制到linux默认库文件/usr/lib/下。

若想在任意库目录下生产可执行文件,去掉步骤2,用gcc -o myhello main.c ./libmyhello.so取代即可。

每次运行myhello 需要保证"./libmyhello.so"的存在,不推荐使用。

 

4:./myhello 提示:

This is main function!
This is hello function!

表示运行成功!

 

GCC生成动态库

标签:

原文地址:http://www.cnblogs.com/kevinhwang/p/5579292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!