2014-11-08 44 views
0

我想執行「一串代碼」如果我編譯與-g標誌(G ++編譯器)的源文件,我想在這樣的事情:允許通過調試標誌的printf

int main() 
{ 
    // do some calculations... 
    #if DEBUG 
     fputs("MATRIX:\n", stdout); 
     Print_Matrix(A, M, N); 

     fputs("VECTOR:\n", stdout); 
     Print_Vector(x, N); 

     fputs("PARALLEL RESULT\n", stdout); 
     Print_Vector(y, M); 

     fputs("SERIAL RESULT\n", stdout); 

     Print_Vector(y_serial, M); 

    #else 
     fprintf(stdout, "SIZE: %d x %d AND %d THREADS\n", M, N, NUM_OF_THREADS); 
     fprintf(stdout, "TIEMPO CONC.:%d mseg\n", (int)final_par); 
     fprintf(stdout, "TIEMPO SERIAL:%d mseg\n", (int)final_serial); 
    #endif 
} 

的目的是當矩陣的大小很小時,我將以DEBUG模式運行,如果沒有,那麼我將打印執行時間(對於較大的矩陣)。

問題是:如果我編譯它有或沒有-g標誌,它從不打印有關矩陣或向量的信息。

+1

加上'-DDEBUG'選項。 – BLUEPIXY 2014-11-08 20:02:19

+1

您是否嘗試過使用'-g -DDEBUG = 1'編譯? – user4815162342 2014-11-08 20:03:17

+1

'-g'不是魔術。它沒有靜靜地定義任何宏(這將是可怕的)。它只在二進制文件中生成調試信息。你需要自己添加宏。 – 2014-11-08 20:03:30

回答

2

如果用DEBUG編譯爲一個標誌,你會執行printf()的,如果不是fprintf()的。

請注意,-g不是我們在這裏關注的,因爲它只會生成調試信息,我們可以在以後使用調試器。


檢查這個簡單的例子。

px.c

#include <stdio.h> 

int main(void) 
{ 
    #if DEBUG 
     printf("Somewhere DEBUG was feeded to this program\n"); 
    #else 
     printf("Somewhere DEBUG was NOT feeded to this program\n"); 
    #endif 
    return 0; 
} 

執行:

[email protected]:~$ gcc -Wall px.c -o myexe 
[email protected]:~$ ./myexe 
Somewhere DEBUG was NOT feeded to this program 

[email protected]:~$ gcc -Wall -DDEBUG px.c -o myexe 
[email protected]:~$ ./myexe 
Somewhere DEBUG was feeded to this program