setting macro

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roman Mashak

    #1

    setting macro

    Hello, All!

    I'm trying to set up macro:

    ....
    int main(int argc, char *argv[], char *envp[])
    {
    ....
    if ( argc > 1 ) {
    if ( strcmp(argv[1], "-d") == 0 ) {
    #define DEBUG
    ...
    }
    }
    ....
    #ifdef DEBUG
    fprintf(stdout, ...);
    #endif
    ....

    }

    So, when I put '-d' switch on a command line, DEBUG should be on and
    available for usage further up in code. But it fails, where did I make bug?
    Thanks.

    With best regards, Roman Mashak. E-mail: mrv@tusur.ru


  • Simon Morgan

    #2
    Re: setting macro

    On Fri, 22 Jul 2005 20:30:58 +0900, Roman Mashak wrote:
    [color=blue]
    > ...
    > int main(int argc, char *argv[], char *envp[]) {
    > ...
    > if ( argc > 1 ) {
    > if ( strcmp(argv[1], "-d") == 0 ) {
    > #define DEBUG
    > ...
    > }
    > }
    > ...
    > #ifdef DEBUG
    > fprintf(stdout, ...);
    > #endif
    > ...
    >
    >
    > }
    > So, when I put '-d' switch on a command line, DEBUG should be on and
    > available for usage further up in code. But it fails, where did I make
    > bug? Thanks.[/color]

    The preprocessor and compiler are two distinct systems so by the time the
    compiler gets around to compiling the above code all of the preprocessor
    directives will have been stripped out. Unless the #ifdef section is part
    of a conditional statement it will always get executed because you've
    defined DEBUG earlier in the program.

    Comment

    • Cong Wang

      #3
      Re: setting macro



      Roman Mashak wrote:[color=blue]
      > Hello, All!
      >
      > I'm trying to set up macro:
      >
      > ...
      > int main(int argc, char *argv[], char *envp[])
      > {
      > ...
      > if ( argc > 1 ) {
      > if ( strcmp(argv[1], "-d") == 0 ) {
      > #define DEBUG
      > ...
      > }
      > }
      > ...
      > #ifdef DEBUG
      > fprintf(stdout, ...);
      > #endif
      > ...
      >
      > }
      >
      > So, when I put '-d' switch on a command line, DEBUG should be on and
      > available for usage further up in code. But it fails, where did I make bug?
      > Thanks.
      >
      > With best regards, Roman Mashak. E-mail: mrv@tusur.ru[/color]

      You can not do that.Because macros are proccessed before compiling.In
      fact,whether strcmp(argv[1], "-d") == 0 or not,DEBUG all are defined!So
      you make a bug!

      Comment

      • David Resnick

        #4
        Re: setting macro

        Roman Mashak wrote:[color=blue]
        > Hello, All!
        >
        > I'm trying to set up macro:
        >
        > ...
        > int main(int argc, char *argv[], char *envp[])
        > {
        > ...
        > if ( argc > 1 ) {
        > if ( strcmp(argv[1], "-d") == 0 ) {
        > #define DEBUG
        > ...
        > }
        > }
        > ...
        > #ifdef DEBUG
        > fprintf(stdout, ...);
        > #endif
        > ...
        >
        > }
        >
        > So, when I put '-d' switch on a command line, DEBUG should be on and
        > available for usage further up in code. But it fails, where did I make bug?
        > Thanks.
        >
        > With best regards, Roman Mashak. E-mail: mrv@tusur.ru[/color]

        #define is a preprocessor command, NOT a runtime one.
        The code shown will have DEBUG defined below the point in the
        code where you have the #define... It matters not whether you
        invoke the program with "-d". You use the preprocessor in this
        way to include or omit sections of code, not to make runtime
        choices available.

        You could do the same with a static, as in

        static int debug_on = 0;
        (at file scope)

        if ( strcmp(argv[1], "-d") == 0 ) {
        debug_on = 1;
        }

        Then use debug_on to determine if you are debugging.

        -David


        -David

        Comment

        • Suman

          #5
          Re: setting macro


          Roman Mashak wrote:[color=blue]
          > Hello, All!
          >
          > I'm trying to set up macro:
          >
          > ...
          > int main(int argc, char *argv[], char *envp[])
          > {
          > ...
          > if ( argc > 1 ) {
          > if ( strcmp(argv[1], "-d") == 0 ) {
          > #define DEBUG
          > ...
          > }
          > }
          > ...
          > #ifdef DEBUG
          > fprintf(stdout, ...);
          > #endif
          > ...
          >
          > }
          >
          > So, when I put '-d' switch on a command line, DEBUG should be on and
          > available for usage further up in code. But it fails, where did I make bug?
          > Thanks.[/color]

          So, I wrote a small program that might help:
          /*-----start-----*/
          #include <stdio.h>

          int main(int argc, char *argv[])
          {
          #ifdef DOIT
          printf("Macro:\ n");
          #endif
          if ( argc == 2 )
          {
          if ( argv[1][1] == 'd' )
          {
          printf("command line option:\n");
          }
          }
          else
          {
          printf("only other usage: ./test -d\n");
          }
          return 0;
          }

          /*-----end-----*/

          And compiled-and-ran with gcc 4.0.0 in the following way:

          1. The macro is undefined now:
          a-power-mac-g5:~ kr$ gcc -std=c99 -Wall -W -pedantic test.c -o test
          a-power-mac-g5:~ kr$ ./test
          only other usage: ./test -d
          a-power-mac-g5:~ kr$ ./test -d
          command line option:
          a-power-mac-g5:~ kr$

          2. The macro is defined now:
          a-power-mac-g5:~ kr$ gcc -DDOIT=1 -std=c99 -Wall -W -pedantic test.c -o
          test
          a-power-mac-g5:~ kr$ ./test
          Macro:
          only other usage: ./test -d
          a-power-mac-g5:~ kr$ ./test -d
          Macro:
          command line option:
          a-power-mac-g5:~ kr$

          Of-course, setting macros like that[2] is not guaranteed for all other
          compilers.
          So RTFM ;)

          Comment

          • Suman

            #6
            Re: setting macro



            Roman Mashak wrote:[color=blue]
            > Hello, All!
            >
            > I'm trying to set up macro:
            >
            > ...
            > int main(int argc, char *argv[], char *envp[])
            > {
            > ...
            > if ( argc > 1 ) {
            > if ( strcmp(argv[1], "-d") == 0 ) {
            > #define DEBUG
            > ...
            > }
            > }
            > ...
            > #ifdef DEBUG
            > fprintf(stdout, ...);
            > #endif
            > ...
            >
            > }
            >
            > So, when I put '-d' switch on a command line, DEBUG should be on and
            > available for usage further up in code. But it fails, where did I make bug?
            > Thanks.
            >[/color]

            You have it all messed up :) Or, is it me?
            If you are trying to get a command line argument and use it to change
            the flow, use if-else construct or friends.

            If you are trying to set a Macro value, using command line,
            it has to be during compiler(prepro cessor actually) invocation,
            not at runtime. Check manuals for your compiler/IDE whatever, on how
            to do such things.

            Comment

            • Keith Thompson

              #7
              Re: setting macro

              Simon Morgan <me@privacy.net > writes:[color=blue]
              > On Fri, 22 Jul 2005 20:30:58 +0900, Roman Mashak wrote:
              >[color=green]
              >> ...
              >> int main(int argc, char *argv[], char *envp[]) {
              >> ...
              >> if ( argc > 1 ) {
              >> if ( strcmp(argv[1], "-d") == 0 ) {
              >> #define DEBUG
              >> ...
              >> }
              >> }
              >> ...
              >> #ifdef DEBUG
              >> fprintf(stdout, ...);
              >> #endif
              >> ...
              >>
              >>
              >> }
              >> So, when I put '-d' switch on a command line, DEBUG should be on and
              >> available for usage further up in code. But it fails, where did I make
              >> bug? Thanks.[/color]
              >
              > The preprocessor and compiler are two distinct systems so by the time the
              > compiler gets around to compiling the above code all of the preprocessor
              > directives will have been stripped out. Unless the #ifdef section is part
              > of a conditional statement it will always get executed because you've
              > defined DEBUG earlier in the program.[/color]

              That's not quite it. The preprocessor is actually part of the
              compiler. It may be implemented as a separate program, but that's an
              irrelevant implementation detail.

              It's true that preprocessing is an early phase of compilation, and
              that preprocessing is completed before the compiler examines
              constructs like declarations and statements, but that's not really the
              point here.

              The point is that preprocessor directives are handled during
              compilation, and the "-d" argument is handled when the program is
              executed. The running program cannot affect the way preprocessing
              directives are handled.

              If you want to make decisions during execution based on command-line
              arguments, you'll need to use if statements, not #if or #ifdef
              directives.

              (Also, the additional envp argument, though it's a common extension,
              is non-standard. The standard getenv() function provides limited
              access to environment variables. If that's not sufficient, many
              systems provide things like a putenv() function and/or an external
              variable called "environ".)

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              Working...