0

I want to execute "a bunch of code" if I compiled the source file with the -g flag (g++ compiler), I was thinking in something like this:

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
}

The purpose is that I will run in DEBUG mode when the size of the matrix is small, if not, then I will print the execution times (for bigger matrices).

The problem is: if I compile it with or without the -g flag, it never prints the info about the matrix or the vector.

FacundoGFlores
  • 7,858
  • 12
  • 64
  • 94

1 Answers1

2

If you compile with DEBUG as a flag, you'll execute the printf()'s, if not the fprintf()'s.

Notice that -g is not the one to focus on here, since it will only generate debug info, which we can take advantage of later with a debugger.


Check this simple example.

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;
}

Execution:

samaras@samaras-A15:~$ gcc -Wall px.c -o myexe
samaras@samaras-A15:~$ ./myexe
Somewhere DEBUG was NOT feeded to this program

samaras@samaras-A15:~$ gcc -Wall -DDEBUG px.c -o myexe
samaras@samaras-A15:~$ ./myexe
Somewhere DEBUG was feeded to this program
gsamaras
  • 71,951
  • 46
  • 188
  • 305