作者ric2k1 (Ric)
看板EE_DSnP
标题Re: [问题] g++
时间Sun Oct 6 22:26:31 2013
如果你的程式只有一个 .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
:
: ----------------
:
: 先编成.o档 v.s. 直接形成执行档
:
: 两个有差吗?
:
: --
:
※ 发信站: 批踢踢实业坊(ptt.cc)
: ◆ From: 111.249.121.121
: 推 ypf791:大型程式如果每次都重新编译所有.o档会等到天荒地老.... 10/06 11:03
: → ColaNut:那-g -Wall通常会加在哪?还是两个都会加阿? 10/06 11:14
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 111.248.105.212
※ 编辑: ric2k1 来自: 111.248.105.212 (10/07 00:58)
1F:推 ColaNut:呜呜,写写老师打这麽多~ 10/07 07:35