mac系統(tǒng)
提前準備好test.c文件,內(nèi)容如下
include<stdio.h>
int main(){
printf("hello world!\n");
return 0;
}
打開終端,切到test.c文件所在目錄
一、編譯流程
1、預處理
gcc -E test.c -o test.i
2、編譯
gcc -S test.i -o test.s
3、匯編
gcc -c test.s -o test.o
4、鏈接
gcc test.o -o test
最后生成的為可執(zhí)行文件,輸入
./test
輸出
hello world!
二、生成靜態(tài)庫
1、生成目標文件
gcc -c test.c -o test.o
2、使用ar命令將目標文件打包成靜態(tài)庫
ar rcs libtest.a test.o
三、生成動態(tài)庫
1、生成目標文件
gcc -c test.c
2、使用-fPIC和-shared生成動態(tài)庫
gcc -shared -fPIC -o libtest.so test.o