0

Is it possible to declare static variable in a #define directive?

    // header file
    #define TEXT_ENUM
    #ifdef TEXT_ENUM
        #define TEXT_HANDLING_MACRO  \
             static const char * TEXT[]; \
             static const char * getText( int _enum ) { \
                 return TEXT[_enum]; \
             }
    #else
        #define TEXT_HANDLING_MACRO
    #endif

    struct Foo {
        TEXT_HANDLING_MACRO
    };

   // cpp file
   #include "foo.h"

   const char * Foo::TEXT[] = { 
       "ONE",
       "TWO",
       "THREE",
       0
   };

How compiler will resolve static const char *, when I include this header file in some other file and try to access Foo::TEXT[].

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Oli
  • 1,313
  • 14
  • 31
  • Did you mean `Foo::TEXT[]`? It should work then. – πάντα ῥεῖ Oct 23 '16 at 16:29
  • I get undefined reference to `Packet::TEXT' error. – Oli Oct 23 '16 at 16:31
  • 2
    @has: That means that your `.cpp` file is missing. In what you provided above `Packet::TEXT` is perfectly defined. – AnT stands with Russia Oct 23 '16 at 16:31
  • @πάντα ῥεῖ, These files are present in Arduino Library, In arduino code is compiling file. I need these files in QT project, so what I did is, I add INCLUDEPATH += /home/has/Arduino/libraries/ in Qt project file. I did get link to header file. But somehow it is not picking cpp file. – Oli Oct 23 '16 at 16:34
  • _@has_ What @AnT said. Also fix your example in the question please! – πάντα ῥεῖ Oct 23 '16 at 16:35
  • 1
    i don't know how the qt creator toolchain works precisely (if that's what you are using), but it should be using CMake. Are you sure it is in `set(SOURCE_FILES ...)` ? – asu Oct 23 '16 at 16:35
  • @πάντα ῥεῖ, thanks alot, I had to do both INCLUDEPATH and SOURCE+= filePAth.cpp, to get it working. – Oli Oct 23 '16 at 16:46
  • @ANT thanks a lot for your reply. – Oli Oct 23 '16 at 16:46

2 Answers2

1

Is it possible to declare static variable in a #define directive?

Yes, it is possible.

How compiler will resolve static const char *, when I include this header file in some other file and try to access Foo::TEXT[].

It's resolved at the linking stage.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

You have to figure out what happens through the stages of compilations that the C compiler uses.

Since #define si a pre-compiler directive, everything will be resolved before actually compiling or looking at the code. Pieces of text (code, functions, whatever is included) will be passed on or filtered according to the directive.

All the rest happens after that, like compilation, that will look for global variable declaration, and linking, that will look for the address of those variable.

In you case, if you take your file and compile with a gcc compiler and the -E option (to do just the precompiler stage), you will get:

    struct Foo {
        static const char * TEXT[]; static const char * getText( int _enum ) {   return TEXT[_enum]; }
    };

   const char * Foo::TEXT[] = {
       "ONE",
       "TWO",
       "THREE",
       0
   };
bracco23
  • 2,181
  • 10
  • 28