上一篇:
程序猿装B指南

gcc 静态编译
就是在编译的时候把你所有的模块和库文件编译到一个可执行文件中,当你启动这个程序的时候所有模块和库加载到内存。加快程序执行效率,
优点:速度快,依赖性低
缺点:体积大,加载慢
This is The C Code
[root@74-82-173-217 static]# cat add.c int add (int x, int y) {
return x + y;
}
Parsed in 0.004 seconds at 20.43 KB/s
This is The C Code
[root@74-82-173-217 static]# cat print.c #include <stdio.h>
void print (int x) {
printf ("%d\n",x);
}
Parsed in 0.004 seconds at 24.40 KB/s
This is The C Code
[root@74-82-173-217 static]# cat head.h #ifndf HEAD_H
#define HEAD_H
int add (int ,int);
void print (int);
#endif
Parsed in 0.004 seconds at 26.14 KB/s
This is The C Code
[root@74-82-173-217 static]# cat main.c #include <stdio.h>
#include "head.h"
int main () {
int x = add(3, 5);
print(x);
}
Parsed in 0.004 seconds at 32.77 KB/s
1、编译
[root@74-82-173-217 static]# gcc -c add.c print.c
编译目标文件 add.c print.c 生成 add.o print.o
2、生成静态库
[root@74-82-173-217 static]# ar -r lib.a add.o print.o
使用 ar 命令生成 lib.a 静态库
3、生成执行文件
[root@74-82-173-217 static]# gcc -static main.c lib.a -o main
使用 gcc -static 参数 加载lib.a 静态库生成 main 执行文件

最后编辑: selboo 编辑于2012/05/08 15:35