XCode Preprocessor Declarations for String Aliases 
I recently ran into a problem with adding some preprocessor declarations to an XCode project. Basically I needed to make the equivalent of a #defined symbol but declared within the project rather than source (a pretty common thing to do).

Googling quickly identified the project setting "Preprocessor Macros" as being the way to do it. It maps to the command line GCC option "GCC_PREPROCESSOR_DEFINITIONS".

The documentation says to specify the symbol and if it's an alias simply list the symbol followed by the equal sign and the value.

Like this:

SYMBOL=FOO

In my case, I needed the symbol to be a string.

Like this:

SYMBOL="foo"

This is where I ran into problems. When I attempted to compile, I got errors that indicated that my quotes had disappeared by the time the preprocessor got the declaration. So I figured this should be a simple solution. I just needed to figure out how XCode expects me to escape the quotes.

I tried everything to figure out what the stupid escape was. I tried the standard C-style /", double-quotes, two single quotes, @"STRING", HTML tags, etc. NOTHING worked. I tried Googling for solutions but came up empty too.

Finally, I had my eureka moment. I'll just create my own "stringify" macro function and pray I can define macro functions in XCode.

If declared in C(++), it would look like this:

STRINGIFY(X) #X

The # sign has special meaning inside preprocessor macros and converts the succeeding argument to an encapsulated string.

The full solution is to first define STRINGIFY under XCode's "Preprocessor Macros" and then use that to define your alias declaration.

Like this:

STRINGIFY(X)=#X SYMBOL=STRINGIFY(foo)

And it worked! I don't feel too bad about this being a bit hackish because the alternative would be modifying a lot of source code. ;)

[ add comment ] ( 3 views )   |  [ 0 trackbacks ]   |  permalink

<<First <Back | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Next> Last>>