如何使用gcc_clang進行C語言的編譯_編譯的流程是什麼?

編譯命令

gcc/clang -g -O2 -o -c test test.c -I... -L... -l
-g : 輸出文件中的調試信息
-O : 對輸出文件作出指令優化,默認是O1, O2優化更多
-c : 能夠編譯成
-o : 輸出文件
-I : 指定頭文件
-L : 指定庫文件位置
-l : 具體使用哪些庫

編譯流程

  1. 預編譯
  2. 編譯
  3. 連接, 動態連接/靜態連接

編寫文件 add.cshell

#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.hcode

int add(int a, int b);

編寫最終程序io

#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開頭和結尾的.aclass

clang -g -o testlib testlib.c -I . -L . -lmylib // -I . 頭文件在當前目錄的意思, -L . -lmylib是指定文件的意思
相關文章
相關標籤/搜索