[問題] 不用recompile就可決定是否加debug info

看板C_and_CPP作者 (不停向前看的生活)時間14年前 (2011/11/18 00:40), 編輯推噓1(1012)
留言13則, 7人參與, 最新討論串1/2 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Linux 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 來源:Linux程式設計教學手冊第十章 Debugging 方法:Debugging without Recompiling 描述: Before we move on, it's worth mentioning that there's a way of using the printf function to help with debugging without using the #ifdef DEBUG technique, which requires a program to be recompiled before it can be used. The method is to add a global variable as a debug flag, allow a -d option at the command line, which allows the user to switch debugging on even after the program has been released, and add a debug logging function. Now you can intersperse things like this in the program code: if (debug) { sprintf(msg, ...) write_debug(msg) } 補充說明(Supplement): 一直搞不懂要怎麼用 是gcc -o program source_code.c 然後執行時 , 在命令列加-d要怎麼用呢? 我的嘗試如下: (1)在file.c宣告一個全域變數 比如 int debug = 0; ... (2)把檔案編譯完成後,下./program -d debug=1 不能使用…不知怎麼用才對,謝謝指教 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.34.225.172 ※ 編輯: phyllispig 來自: 114.34.225.172 (11/18 00:42)

11/18 00:45, , 1F
看起來要自己檢查argv的值
11/18 00:45, 1F

11/18 00:46, , 2F
書只說了那幾句跟片斷程式,覺得自己理解力好差(抱頭)
11/18 00:46, 2F

11/18 00:50, , 3F
http://codepad.org/bbNkx2vh 大概是這種感覺
11/18 00:50, 3F

11/18 00:51, , 4F
不過覺得這種程式應該會稍微影響效能...
11/18 00:51, 4F

11/18 00:51, , 5F
char **argv ←
11/18 00:51, 5F

11/18 00:53, , 6F
我寫錯了 XD 感謝板主糾正
11/18 00:53, 6F

11/18 00:57, , 7F
謝謝樓上,這確定是一個方法了
11/18 00:57, 7F
※ 編輯: phyllispig 來自: 114.34.225.172 (11/18 00:58)

11/18 13:34, , 8F
我還以為是不用重編譯就能加入 dwarf 之類的 debug info..
11/18 13:34, 8F

11/18 14:04, , 9F
不可能的任務吧XD
11/18 14:04, 9F

11/18 16:32, , 10F
標題很讓人誤導阿...
11/18 16:32, 10F

11/18 16:57, , 11F
標題我也以為是什麼神之寫法....
11/18 16:57, 11F

11/18 18:25, , 12F
把"加"改成"印"應該就不會這麼令人誤解了吧...
11/18 18:25, 12F

11/18 18:25, , 13F
(我一開始也在想到底是什麼東西 @.@)
11/18 18:25, 13F
文章代碼(AID): #1EnJZtuQ (C_and_CPP)
文章代碼(AID): #1EnJZtuQ (C_and_CPP)