Re: [問題] g++

作者: ric2k1 (Ric)   2013-10-06 22:26:31
如果你的程式只有一個 .cpp 檔,
先 compile 成 .o 檔再 compile 程執行檔是沒有差的 (well, 後者多一步啦!)
但如果你有多個 .cpp 檔,直接用 -o 再接所有 .cpp 檔的話,
每次都會把所有的 .cpp 檔都重新 compile 一次,
這樣對於大型程式在做 incremental fixing/enhancement 的時候是會很浪費時間的。
所以,對於多重 .cpp 檔你最好先把每個 .cpp 檔 compile 成 .o 檔
(g++ -c; 並且檢查 dependency, 以減少重新 compile 的檔案數目),
然後再 link 成執行檔。
至於 -g -Wall 要放 g++ 的哪裡,答案是隨便,
請看一下 g++ 的 usage (man g++):
====
NAME
gcc - GNU project C and C++ compiler
SYNOPSIS
gcc [-c|-S|-E] [-std=standard]
[-g] [-pg] [-Olevel]
[-Wwarn...] [-pedantic]
[-Idir...] [-Ldir...]
[-Dmacro[=defn]...] [-Umacro]
[-foption...] [-mmachine-option...]
[-o outfile] infile...
Only the most useful options are listed here; see below for the remain-
der. g++ accepts mostly the same options as gcc.
====
By convention, 方括弧 "[ ]" 內的參數為 optional (可有可無),
optional arguments 之間的順序可以任意調換。
當然,"[-o outfile]" 表示 -o 後面的字串會被視為是 output file,
所以不可以在 -o 後面隨便插入其他參數,否則會被當作檔名。
但對於 compile & linking 來說,
"-g -Wall" 應該是要放在 -c (compile) 的時候來生成 debug information,
linking 時可以不用放。
※ 引述《ColaNut (可樂果)》之銘言:
: 標題: [問題] g++
: 時間: Sun Oct 6 10:57:17 2013
:
: (舉手)
:
: g++ -c a.cpp
: g++ -o out a.o
:
: v.s.
:
: g++ -o out a.cpp
:
:
作者: ColaNut   0000-00-00 00:00:00
嗚嗚,寫寫老師打這麼多~

Links booklink

Contact Us: admin [ a t ] ucptt.com