編譯命令
gcc/clang -g -O2 -o -c test test.c -I... -L... -l
-g : 輸出檔案中的除錯資訊
-O : 對輸出檔案做出指令優化,默認是O1, O2優化更多
-c : 可以編譯成
-o : 輸出檔案
-I : 指定頭檔案
-L : 指定庫檔案位置
-l : 具體使用哪些庫
編譯流程
- 預編譯
- 編譯
- 鏈接, 動態鏈接/靜態鏈接
撰寫檔案 add.c
#include <stdio.h>
int add(int a, int b)
{
return (a+b);
}
clang -g -c add.c // 生成一個指定的add.o的檔案
libtool -static -o libmylib.a add.o // 生成一個libmylib.a的檔案,必須要lib開頭
撰寫檔案 add.h
int add(int a, int b);
撰寫最終程式
#include <stdio.h>
#include "add.h"
int main(int argc, char *argv[])
{
int c = add(1, 2);
printf("c: %d", c)
return 0;
}
clang -g -o testlib testlib.c -I . -L . -lmylib
最終生成 testlib 的檔案, libmylib.a 的庫必須要去掉 lib開頭和結尾的.a
clang -g -o testlib testlib.c -I . -L . -lmylib // -I . 頭檔案在當前目錄的意思, -L . -lmylib是指定檔案的意思
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/54957.html
標籤:C
