gcc -c 檔案
只編譯生成.o不鏈結
gcc -o 檔案(可執行檔名) 檔案(.o)
鏈結生成可執行檔案
下面是簡單例子:
fun.h
void fun();
fun.c
dfun.c#include#include"fun.h"
void fun()
#include#include"fun.h"
int main(int argc,char *argv)
emtt@laptop-nqrndmg3 ~/c
$ gcc -ov hello hello.c
hello:在函式『maincrtstartup』中:
/usr/src/debug/cygwin-2.6.0-1/winsup/cygwin/crt0.c:21: multiple definition of `maincrtstartup'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/../../../../lib/crt0.o:/usr/src/debug/cygwin-2.6.0-1/winsup/cygwin/crt0.c:21:第一次在此定義
hello:在函式『maincrtstartup』中:
/usr/src/debug/cygwin-2.6.0-1/winsup/cygwin/crt0.c:21: multiple definition of `winmaincrtstartup'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/../../../../lib/crt0.o:/usr/src/debug/cygwin-2.6.0-1/winsup/cygwin/crt0.c:21:第一次在此定義
hello:cygming-crtbegin.c:(.text+0x60): multiple definition of `__gcc_register_frame'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/crtbegin.o:cygming-crtbegin.c:(.text+0x10):第一次在此定義
hello:cygming-crtbegin.c:(.text+0xd0): multiple definition of `__gcc_deregister_frame'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/crtbegin.o:cygming-crtbegin.c:(.text+0x80):第一次在此定義
hello:crt0.c:(.data+0x0): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/crtbegin.o:cygming-crtbegin.c:(.data+0x0):第一次在此定義
hello:crt0.c:(.text+0x50): multiple definition of `.weak._jv_registerclasses.__dso_handle'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/crtbegin.o:cygming-crtbegin.c:(.text+0x0):第一次在此定義
/tmp/cckab6kf.o:hello.c:(.text+0x0): multiple definition of `main'
hello:hello.c:(.text+0xe0):第一次在此定義
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/crtbegin.o:cygming-crtbegin.c:(.text+0x73):對『_jv_registerclasses』未定義的引用
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/crtbegin.o:cygming-crtbegin.c:(.text+0x73): relocation truncated to fit: r_x86_64_pc32 against undefined symbol `_jv_registerclasses'
collect2: 錯誤:ld 返回 1
emtt@laptop-nqrndmg3 ~/c
$ vim fun.c
emtt@laptop-nqrndmg3 ~/c
$ vim fun.h
emtt@laptop-nqrndmg3 ~/c
$ vim fun.c
emtt@laptop-nqrndmg3 ~/c
$ vim dfun.c
emtt@laptop-nqrndmg3 ~/c
$ gcc -c fun.c dfun.c
emtt@laptop-nqrndmg3 ~/c
$ ls -l
總用量 76
-rw-r--r-- 1 emtt none 89 5月 19 13:23 dfun.c
-rw-r--r-- 1 emtt none 764 5月 19 13:23 dfun.o
-rw-r--r-- 1 emtt none 91 5月 19 13:22 fun.c
-rw-r--r-- 1 emtt none 12 5月 19 13:21 fun.h
-rw-r--r-- 1 emtt none 838 5月 19 13:23 fun.o
-rw-r--r-- 1 emtt none 83 5月 19 13:12 hello.c
-rwxr-xr-x 1 emtt none 62465 5月 19 13:13 hello.exe
emtt@laptop-nqrndmg3 ~/c
$ gcc -o ddfun dfun.o fun.o
emtt@laptop-nqrndmg3 ~/c
$ ls -l
總用量 140
-rwxr-xr-x 1 emtt none 62753 5月 19 13:24 ddfun.exe
-rw-r--r-- 1 emtt none 89 5月 19 13:23 dfun.c
-rw-r--r-- 1 emtt none 764 5月 19 13:23 dfun.o
-rw-r--r-- 1 emtt none 91 5月 19 13:22 fun.c
-rw-r--r-- 1 emtt none 12 5月 19 13:21 fun.h
-rw-r--r-- 1 emtt none 838 5月 19 13:23 fun.o
-rw-r--r-- 1 emtt none 83 5月 19 13:12 hello.c
-rwxr-xr-x 1 emtt none 62465 5月 19 13:13 hello.exe
emtt@laptop-nqrndmg3 ~/c
$ ddfun
-bash: ddfun: 未找到命令
emtt@laptop-nqrndmg3 ~/c
$ ./ddfun
你好!這是函式呼叫!
gcc 引數說明
l引數就是用來指定程式要鏈結的庫,l引數緊接著就是庫名,那麼庫名跟真正的庫檔名有什麼關係呢?就拿數學庫來說,他的庫名是m,他的庫檔名是libm.so,很容易看出,把庫檔名的頭lib和尾.so去掉就是庫名了 好了現在我們知道怎麼得到庫名,當我們自已要用到乙個第三方提供的庫名字libtest.so,那麼...
gcc 引數說明
g 產生帶除錯資訊的編譯後目標 供偵錯程式 gdb 使用 gcc g source.c c 預處理時不刪除注釋資訊 配合 e使用 o 使用編譯優化級別1編譯程式。o後還可附帶其他的數值,表示不同的優化級別。級別越大優化效果越好,但編譯時間越長 o0 無優化 預設 o和 o1 優化可執行檔案大小及執行...
gcc常見引數說明
1.gcc包含的c c 編譯器 gcc,cc,c g gcc和cc是一樣的,c 和g 是一樣的。一般c程式就用gcc編譯,c 程式就用g 編譯。2.gcc的基本用法 gcc test.c這樣將編譯出乙個名為a.out的程式 gcc test.c o test這樣將編譯出乙個名為test的程式,o引數...