Find the answer to your Linux question:
Results 1 to 3 of 3
Hello. I'm fairly new to C, Linux and Makefiles. I need some help with an issue involving all three. In the following situation, (if I understand what I read), the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    25

    C, Makefiles, and compiling debug info into your code


    Hello. I'm fairly new to C, Linux and Makefiles. I need some help with an issue involving all three.

    In the following situation, (if I understand what I read), the code between the #if..#endif should be compiled, but the while loop will not execute. When I try to run it, it seems that the code between the #if..#endif is not executing (or maybe just not being compiled). Am I doing something wrong? Or am I understanding the documentation wrong?

    Compile statement (notice the -DDELAY=0):
    Code:
    /usr/local/mpi/bin/mpicc  -Wall -g -DDELAY=0  -I/home/schwarz/sundials/instdir/include -I/usr/local/mpi/bin/../include -c FPU.c
    /usr/local/mpi/bin/mpicc -o FPU FPU.o  -Wall -g -DDELAY=0  -L/home/schwarz/sundials/instdir/lib -lsundials_cvode -lsundials_nvecparallel -lm  -L/usr/local/mpi/bin/../lib
    C source code (excerpted for brevity):
    Code:
    #if DELAY
      {
        /* Pause execution so gdb can attach. Swiped from open-mpi[DOT]org[SLASH]faq[SLASH][QUESTION]category=debugging#serial-debuggers, number 1 */
        pid_t PID;
        char hostname[256];
        int i=DELAY;
    
        gethostname(hostname, sizeof(hostname));
        PID=getpid();
        printf("PID %d on %s waiting for gdb to attach:\n", PID=getpid(), hostname);
        fflush(stdout);
    
        while (i>0) {
          printf("PID %d on %s will wait  for `gdb` to attach for %d more iterations.\n", PID, hostname, i);
          fflush(stdout);
          sleep(5);
          i--;
        }
      }
    #endif
    When I execute this system, there is no output from the line printf("PID %d on %s waiting for gdb to attach:\n", PID=getpid(), hostname);. But shouldn't there be some output?

    The goal is to get the printf (which contains debugging info), without waiting. Basically, the same as if I changed the while loop to while (i>1)... and compiled with -DDELAY=1.
    Last edited by StupidUser; 10-19-2012 at 05:14 AM.

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,279
    No, it doesn't get compiled. #if and #endif are preprocessor directives (as are all commands that begin with #); if the condition holds, the preprocessor snips out the code inbetween and the compiler never sees it.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,753
    Quote Originally Posted by hazel View Post
    No, it doesn't get compiled. #if and #endif are preprocessor directives (as are all commands that begin with #); if the condition holds, the preprocessor snips out the code inbetween and the compiler never sees it.
    So, if you had compiled this with the -DDELAY=1 in the code, it should work fine, although the while() loop will also execute, after the text is output. However, as hazel noted, if you don not define DELAY, then the entire block of code between #if DELAY .... #endif will never be seen by the compiler. Be careful of macros. They can be a snake pit - and I speak of an expert snake handler in this regard, having written many complex (multi-page in some cases) functional macros. Very useful for generic programming in C and some older (pre-STL) C++ dialects.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •