[code小心得]程式除錯和gcc 巨集使用技巧

大家好 老樣子開頭 小弟又來嘴炮了 。

gcc 編譯過程中,會產生一些巨集。
可以使用這些巨集 分別列印當前原始檔的資訊。
ex:
printf("file: %s function:%s  line:%d\n", __FILE__, __FUNCTION__, __LINE__);
以上可列印
__FILE__: 表示當下編譯執行的成是檔案 檔名,字串型( char *)
__FUNCTION__: 表示當下執行的函數,字串型( char *)
__LINE__: 表示當下的函數行,整數型 (int)

相關的在這個網頁有提到
http://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Function-Names.html


#字串化 運算子
##連結運算子


#define test(x) test ## x void test1(int a) { printf("Test 1 integer: %d \n",a); } void test2(char * s) { printf("Test 2 String: %s \n",s); } int main(void) { test(1) (100); test2(2) ("hello"); return 0; } 

PS: ##是C語言 預處理階段的連結運算子,可實現巨集引數的連結。


#define DEBUG_OUT( fmt, args... ) \ { \ printf("File: %s Function:%s Line: %d",__FILE__, __FUNCTION__, __LINE__); \ printf( fmt, ##args );\ }
int a = 100; int b = 200; char *s = "string"; DEBUG_OUT(" a = %d;b = %d \n", a, b); DEBUG_OUT(" a = %x;b = %x \n, a, b"); DEBUG_OUT(" s = %s;\n", s);

留言