stringizing a hex value in the preprocessor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henry Townsend

    #1

    stringizing a hex value in the preprocessor

    Consider the little program below. It simply attempts to determine what
    version of what compiler it was built with via documented preprocessor
    macros and print out the result. When the compiler is gcc it works fine,
    because __VERSION__ is supplied as a string. But identifying the Sun
    compiler is harder because it provides __SUNPRO_C as a numerical value
    (equal in my case to 0x580). My attempt to stringify it produces an error:

    % cc -g -o foo foo.c
    "foo.c", line 13: invalid source character: '#'
    "foo.c", line 13: syntax error before or at: 0x580
    cc: acomp failed for foo.c

    Is there a way to do this?

    Thanks,
    HT

    --------------------------------------------------------------
    #include <stdio.h>

    #if defined(__GNUC_ _)
    #define COMPILER_STRING "gcc " __VERSION__
    #elif defined(__SUNPR O_C)
    #define COMPILER_STRING "SunStudio " #__SUNPRO_C
    #else
    #define COMPILER_STRING "unknown compiler/version"
    #endif

    int main(void)
    {
    printf("Compile d with %s\n", COMPILER_STRING );
    return 0;
    }
  • Suman

    #2
    Re: stringizing a hex value in the preprocessor


    Henry Townsend wrote:[color=blue]
    > Consider the little program below. It simply attempts to determine what
    > version of what compiler it was built with via documented preprocessor
    > macros and print out the result. When the compiler is gcc it works fine,
    > because __VERSION__ is supplied as a string. But identifying the Sun
    > compiler is harder because it provides __SUNPRO_C as a numerical value
    > (equal in my case to 0x580). My attempt to stringify it produces an error:
    >[/color]

    Here's my try:

    #include <stdio.h>

    #define str(x) #x
    #define xstr(x) str(x)
    #define paste(x, y) x ## y

    #define __SUNPRO_C 0x580
    #if defined(__IGNUC __)
    #define COMPILER_STRING "gcc " __VERSION__
    #elif defined(__SUNPR O_C)
    #define COMPILER_STRING xstr(paste(SunS tudio, xstr(__SUNPRO_C )))
    #else
    #define COMPILER_STRING "unknown compiler/version"
    #endif

    int main(void)
    {
    printf("Compile d with %s\n", COMPILER_STRING );
    return 0;
    }

    and this was compiled with gcc -std=c99 -Wall.

    Comment

    • Suman

      #3
      Re: stringizing a hex value in the preprocessor


      Suman wrote:[color=blue]
      > Henry Townsend wrote:[color=green]
      > > Consider the little program below. It simply attempts to determine what
      > > version of what compiler it was built with via documented preprocessor
      > > macros and print out the result. When the compiler is gcc it works fine,
      > > because __VERSION__ is supplied as a string. But identifying the Sun
      > > compiler is harder because it provides __SUNPRO_C as a numerical value
      > > (equal in my case to 0x580). My attempt to stringify it produces an error:
      > >[/color]
      >
      > Here's my try:[/color]

      a tad improved:
      --------------------------------------><----------------------------------------------------------------
      #include <stdio.h>

      #define str(x) #x
      #define xstr(x) str(x)
      #define paste(x, y) x ## y

      #define __SUNPRO_C 0x580
      #if defined(__IGNUC __)
      #define COMPILER_STRING "gcc " __VERSION__
      #elif defined(__SUNPR O_C)
      #define COMPILER_STRING xstr(SunStudio xstr(__SUNPRO_C ))
      #else
      #define COMPILER_STRING "unknown compiler/version"
      #endif

      int main(void)
      {
      printf("Compile d with %s\n", COMPILER_STRING );
      return 0;
      }
      --------------------------------------><----------------------------------------------------------------
      gcc -std=c99 -Wall -pedantic -ansi. And, excuse the ignominious
      __IGNU__.
      I don't have a Sun compiler to play with :)

      HTH

      Comment

      • Henry Townsend

        #4
        Re: stringizing a hex value in the preprocessor

        Suman wrote:[color=blue][color=green]
        >> Here's my try:[/color]
        >
        > a tad improved:
        > [snip][/color]

        Yes, that works. Actually it prints:

        Compiled with SunStudio "0x580"

        and a tweak I made to use the line:

        #define COMPILER_STRING "SunStudio " ## xstr(__SUNPRO_C )

        removes the quotes to give:

        Compiled with SunStudio 0x580

        Which is what I was looking for. Thanks!

        HT

        Comment

        • Raghu

          #5
          Re: stringizing a hex value in the preprocessor

          Here is My try :

          #include <stdio.h>

          #define str(x) #x
          #define paste(x,y) x##y
          #define Mystr(x) str(x)


          #define __SUNPRO_C 0x580

          #if defined(__IGNUC __)

          #define COMPILER_STRING "gcc " __VERSION__

          #elif defined(__SUNPR O_C)

          #define COMPILER_STRING paste("SunStudi o",Mystr(__SUNP RO_C))

          #else

          #define COMPILER_STRING "unknown compiler/version"

          #endif

          int main(void)
          {
          printf("Compile d with %s\n", COMPILER_STRING );
          return 0;
          }


          -- Raghu.


          "Henry Townsend" <henry.townsend @not.here> wrote in message
          news:fuednZi67P _9SxLeRVn-ig@comcast.com. ..[color=blue]
          > Suman wrote:[color=green][color=darkred]
          > >> Here's my try:[/color]
          > >
          > > a tad improved:
          > > [snip][/color]
          >
          > Yes, that works. Actually it prints:
          >
          > Compiled with SunStudio "0x580"
          >
          > and a tweak I made to use the line:
          >
          > #define COMPILER_STRING "SunStudio " ## xstr(__SUNPRO_C )
          >
          > removes the quotes to give:
          >
          > Compiled with SunStudio 0x580
          >
          > Which is what I was looking for. Thanks!
          >
          > HT[/color]


          Comment

          • Hubble

            #6
            Re: stringizing a hex value in the preprocessor

            Except that I would not use small letters for macros. Or did I miss
            something? The solution is good but hard to read for me.

            Hubble

            Comment

            • Maxim Yegorushkin

              #7
              Re: stringizing a hex value in the preprocessor


              Henry Townsend wrote:[color=blue]
              > Consider the little program below. It simply attempts to determine what
              > version of what compiler it was built with via documented preprocessor
              > macros and print out the result. When the compiler is gcc it works fine,
              > because __VERSION__ is supplied as a string. But identifying the Sun
              > compiler is harder because it provides __SUNPRO_C as a numerical value
              > (equal in my case to 0x580). My attempt to stringify it produces an error:[/color]

              Why not just use a simple shell script invoked by makefile to do just
              that?

              $ cat build_string
              #! /bin/bash

              echo "namespace $1 { "
              echo "extern char const build_string[] = "
              echo \"$(date -u +"%F %T UTC") \"
              echo \"$(g++ --version | head -n 1)\"
              echo ";"
              echo "}"

              bash-3.00$ cat Makefile

              ....
              all: app
              app: dirs build_string $(app_obj)
              $(CXX) -o $(bin_dir)app $(app_obj) $(LDFLAGS)
              ....
              ..PHONY : dirs clean build_string install
              build_string:
              ./build_string app > $(src_dir)build _string.cpp
              ....

              Comment

              • Keith Thompson

                #8
                Re: stringizing a hex value in the preprocessor

                "Hubble" <reiner@huober. de> writes:[color=blue]
                > Except that I would not use small letters for macros. Or did I miss
                > something? The solution is good but hard to read for me.[/color]

                What solution?

                Please read <http://cfaj.freeshell. org/google/>.

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

                • Hubble

                  #9
                  Re: stringizing a hex value in the preprocessor

                  Keith Thompson writes:
                  [color=blue]
                  >What solution[/color]
                  [color=blue]
                  >Please read <http://cfaj.freeshell. org/google/>.[/color]

                  Sorry for using google:

                  Suman writes:
                  [color=blue]
                  >#define str(x) #x
                  >#define xstr(x) str(x)
                  >#define paste(x, y) x ## y[/color]

                  I would not use small letters for macros. Or did I miss
                  something? These macros is good but hard to read for me.

                  Hubble

                  Comment

                  • pete

                    #10
                    Re: stringizing a hex value in the preprocessor

                    Hubble wrote:
                    [color=blue]
                    > Suman writes:
                    >[color=green]
                    > >#define str(x) #x
                    > >#define xstr(x) str(x)
                    > >#define paste(x, y) x ## y[/color]
                    >
                    > I would not use small letters for macros. Or did I miss
                    > something? These macros is good but hard to read for me.[/color]

                    The first two of those macros are copied from the C standard.
                    That makes it easy to look them up in the standard
                    if you want to see what the standard has to say about them.
                    Otherwise, neither would I write them that way.

                    --
                    pete

                    Comment

                    • Jordan Abel

                      #11
                      Re: stringizing a hex value in the preprocessor

                      On 2005-12-02, Suman <skarpio@gmail. com> wrote:

                      #ifdef __STDC__[color=blue]
                      > #define str(x) #x
                      > #define paste(x, y) x ## y[/color]
                      #else
                      #define str(x) "x"
                      #define paste(x) x/**/y
                      #endif[color=blue]
                      > #define xstr(x) str(x)[/color]

                      --
                      hey, if you're going to hide them in their own macros, you might as well...

                      Comment

                      • pete

                        #12
                        Re: stringizing a hex value in the preprocessor

                        pete wrote:[color=blue]
                        >
                        > Hubble wrote:
                        >[color=green]
                        > > Suman writes:
                        > >[color=darkred]
                        > > >#define str(x) #x
                        > > >#define xstr(x) str(x)
                        > > >#define paste(x, y) x ## y[/color]
                        > >
                        > > I would not use small letters for macros. Or did I miss
                        > > something? These macros is good but hard to read for me.[/color]
                        >
                        > The first two of those macros are copied from the C standard.
                        > That makes it easy to look them up in the standard
                        > if you want to see what the standard has to say about them.
                        > Otherwise, neither would I write them that way.[/color]

                        I suppose they wouldn't be that difficult to look up
                        if they were in caps and you knew they were in the standard.
                        There's a couple of things
                        that I copy from the C standard in my code.

                        For example

                        int main(int argc, char *argv[])

                        even though

                        int main(int argc, char **argv)

                        is really my prefered style.

                        --
                        pete

                        Comment

                        • Keith Thompson

                          #13
                          Re: stringizing a hex value in the preprocessor

                          "Hubble" <reiner@huober. de> writes:[color=blue]
                          > Keith Thompson writes:
                          >[color=green]
                          >>What solution[/color]
                          >[color=green]
                          >>Please read <http://cfaj.freeshell. org/google/>.[/color]
                          >
                          > Sorry for using google:[/color]
                          [snip]

                          No need to apologize; using google is perfectly acceptable as long as
                          you use it correctly. (Some people have killfiled everything posted
                          through groups.google.c om, but that's their decision.)

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

                          • Suman

                            #14
                            Re: stringizing a hex value in the preprocessor


                            pete wrote:[color=blue]
                            > Hubble wrote:
                            >[color=green]
                            > > Suman writes:
                            > >[color=darkred]
                            > > >#define str(x) #x
                            > > >#define xstr(x) str(x)
                            > > >#define paste(x, y) x ## y[/color]
                            > >
                            > > I would not use small letters for macros. Or did I miss
                            > > something? These macros is good but hard to read for me.[/color]
                            >
                            > The first two of those macros are copied from the C standard.
                            > That makes it easy to look them up in the standard
                            > if you want to see what the standard has to say about them.
                            > Otherwise, neither would I write them that way.[/color]

                            Addendum:
                            The third, is a modified form of what you find in K&R 2:
                            #define paste(front, back) front ## back

                            Comment

                            • pete

                              #15
                              Re: stringizing a hex value in the preprocessor

                              Suman wrote:[color=blue]
                              >
                              > pete wrote:[color=green]
                              > > Hubble wrote:
                              > >[color=darkred]
                              > > > Suman writes:
                              > > >
                              > > > >#define str(x) #x
                              > > > >#define xstr(x) str(x)
                              > > > >#define paste(x, y) x ## y
                              > > >
                              > > > I would not use small letters for macros. Or did I miss
                              > > > something? These macros is good but hard to read for me.[/color]
                              > >
                              > > The first two of those macros are copied from the C standard.
                              > > That makes it easy to look them up in the standard
                              > > if you want to see what the standard has to say about them.
                              > > Otherwise, neither would I write them that way.[/color]
                              >
                              > Addendum:
                              > The third, is a modified form of what you find in K&R 2:
                              > #define paste(front, back) front ## back[/color]

                              That's good enough for me.

                              It might be worth keeping in mind
                              that there's a few deviations here and there in standard C,
                              from what's generally considered good style.
                              FILE is a typedef, stdout is a macro.

                              --
                              pete

                              Comment

                              Working...