1. 程式人生 > >gcc編譯c語言,非Makefile形式

gcc編譯c語言,非Makefile形式

gcc (選項) (引數)

選項:

-o:指定生成的輸出檔案;
-E:僅執行編譯預處理;
-S:將C程式碼轉換為彙編程式碼;
-wall:顯示警告資訊;
-c:僅執行編譯操作,不進行連線操作。

引數:需要編譯的檔案

其中選項和引數位置可調換

 

主函式所在檔案include其他檔案時 gcc編譯時不需要列出已經include的檔案,include可以是*.h,也可以是*.c,但最好不要include*.c。

1.如果main()所在檔案include所有*.c 編譯可用gcc -o test main.c

2.如果main()所在檔案只include a.h,b.h,c.h編譯需要用gcc -o test main.c a.c b.c c.c

3.如果沒有include只有extern,extern的函式或變數在a.c中編譯要用gcc -o test main.c a.c

可參考http://www.cnblogs.com/zhangfeionline/p/5861721.html

兩種多檔案編譯方式

a.如上所寫,gcc中列出多個原始檔一起編譯

b.單個檔案分別編譯為*.o,再將*.o連線

gcc -c testfun.c    #將testfun.c編譯成testfun.o
gcc -c test.c       #將test.c編譯成test.o
gcc -o testfun.o test.o -o test    #將testfun.o和test.o連結成test

參考http://man.linuxde.net/gcc