4 Replies Latest reply on Dec 21, 2016 10:56 AM by user_78878863

    imagecraft macro stringification

    m.capdeville

      Hi,

         

      I need to stringify a macro expansion, as with gnu c preprocessor by prepending it with '#', but this dont work and give the error "illegal character `#' ".

         

      The macro is define in makefile as -DVERSION="$(git describe)"

         

      and used as const char Version[] = #VERSION ;

         

      It seems that stringification is possible since there is a preprocessor error saying "Stringified macro arg is too long" in the documentation, but it may have an other syntax.

         

      Please, is anyone have allready used stringification with Imagecraft compiler (PsocDesigner 5.4).

         

       

         

      Regard.

        • 1. Re: imagecraft macro stringification
          user_78878863

          Just use VERSION - cpp expands all defines and macros when it encounters them, there is no need for a special prefix. The '#' merely tells cpp that now preprocessor directive follows.

             

          I don't think that you can do runtime expansion of command output on Windows, though (on Linux you would do a backtick expression).

          • 2. Re: imagecraft macro stringification
            m.capdeville

            Thank, but I try with this code :
            const char Version[]=VERSION;

               

            the compiler give me the errors :

               

            missing { in initialization of `incomplete array of const char'

               

            undeclared identifier `test01'

               

            initializer must be constant

               

            I want the expantion to have double cote :

               

            const char Version[]="test01";

               

            This is what I get with the # preprocessor operator with gnu cpp. It is called 'stringification operator'.

               

            The runtime expantion work as wanted because it is interpreted by gnu make. I just put the macro define in the project setting:

               

            VERSION="$(shell c:\cygwin\bin\git describe)"

               

            My current git tag is test01, so VERSION expand to it.

            • 3. Re: imagecraft macro stringification
              m.capdeville

              Ok, I've found the solution.

                 

              Stringify operator is working, but only in macro define, and is done before expantion of the arcgument. So, defining a macro for stringification is not sufficient. when writing :

                 

              #define STR(x) #x

                 

              const char Version[]=STR(VERSION);

                 

              the expantion give :

                 

              const char Version[]="VERSION";

                 

              I have to write a second no-op macro to let the preprocessor first expand VERSION to test01, then stringify test01 to "test01":

                 

              #define STR(x)    #x
              #define STRX(x)    STR(x)
              const char Version[]=STRX(VERSION);

                 

              This time preprocessor expant to :

                 

              const char Version[]="test01"

              • 4. Re: imagecraft macro stringification
                user_78878863

                Glad you got it working. And I learned something new today :)