Linux静态链接库和动态链接库

display.h

#ifndef _DISPLAY_H
#define _DISPLAY_H

void display (int);

#endif

display.c

#include <stdio.h>
#include "display.h"

void display ( int n ) { if (0 == n) puts ("0"); else { printf ("%d\n", n--); display (n); } return ; } /* ----- end of function display ----- */

常规编译一下

gcc -c display.c

生成静态链接库

ar rc display.a display.o

生成动态链接库

gcc -fPIC -shared -o display.so display.o

 



 

 

测试文件test.c

#include <stdlib.h>
#include "display.h"
int main ( int argc, char *argv[] ) { display (5); return EXIT_SUCCESS; } /* ---------- end of function main ---------- */

 使用静态链接库编译测试文件

gcc test.c -o static_test ./display.a

使用动态链接库编译测试文件

gcc test.c -o dynamic_test ./display.so

结果:

[leajon@arch library]$ ./static_test
5
4
3
2
1
0
[leajon@arch library]$ ./dynamic_test
5
4
3
2
1
0
[leajon@arch library]$ 

 

posted @ 2012-05-08 14:12  Leo Forest  阅读(217)  评论(0编辑  收藏  举报