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

看板C_and_CPP作者時間14年前 (2011/11/18 20:21), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
我想應該是像這個樣子: --- work.c #include <stdio.h> //#define DEBUG_L1 //#define DEBUG_L2 int workload( void) { int start, work; #ifdef DEBUG_L1 printf("Debug stage Level 1: start.\n"); #endif start = 1; work = ( start << 7) - 3; #ifdef DEBUG_L2 printf("start: %d\twork: %d\n", start, work); #endif #ifdef DEBUG_L1 printf("Debug stage Level 1: finished.\n"); #endif return work; } int main ( void) { printf("result: %d\n", workload()); return 0; } --- % gcc work.c % ./a.out result: 125 % gcc -DDEBUG_L1 work.c % ./a.out Debug stage Level 1: start. Debug stage Level 1: finished. result: 125 % gcc -DDEBUG_L2 work.c % ./a.out start: 1 work: 125 result: 125 % gcc -DDEBUG_L1 -DDEBUG_L2 work.c % ./a.out Debug stage Level 1: start. start: 1 work: 125 Debug stage Level 1: finished. result: 125 --- 在gcc compile 時可以加入參數 -D 後面可以宣告macro 就像上面compile 的時候加上 -DDEBUG_L1 就像是在code裡面加上: #define DEBUG_L1 就能印出 #ifdef DEBUG_L1 ... #endif 中的部分 若是要定義數字, 比如說: char str[MAX_LENGTH]; 可以用 gcc -DMAX_LENGTH=128 preprocessor 完就會變成 char str[128]; 簡單來說就是使用define 來定義debug label 然後靠compiler 加入macro 來觸發debug label 大概是這個意思;; -- The Net has me._ --- 我發完才發現跟問得相反了 XDrz ※ 編輯: terces 來自: 140.123.103.42 (11/18 20:26)

11/19 18:11, , 1F
其實一般是用define,不然就是verbose
11/19 18:11, 1F
文章代碼(AID): #1EnatYKJ (C_and_CPP)
文章代碼(AID): #1EnatYKJ (C_and_CPP)