Rob
Rob

Reputation: 947

MSVC: how to include the # character in the value of a pre-processor macro passed from the command-line

We build/run our embedded C code on MSVC as a convenient development/test environment.

Like most C compilers, MSVC allows the use of -D to define pre-processor macros on the command line, i.e. CL -DTHING=somechars somefile.c will cause somefile.c to see #define THING somechars. Our problem is that we need to pass in a pre-processor macro where the value contains the # character (it is used in a password that we are testing our embedded C code with and so has to be built into the embedded test code, we're stuck with it), i.e. we want to do CL -DTHING=1234# somefile.c so that somefile.c sees #define THING 1234#.

However MSVC uses # as a substitute for =, and so what somefile.c ends up seeing is #define THING 1234=; our password is now wrong.

Does anyone know if there's a way to stop MSVC doing this!?

I've tried escaping the value by putting ## instead of # but that comes out as =#, which is not much better; I guess MSVC realizes there can't be two = signs but my string is still wrong and there's nothing sensible I can do in the embedded C code to recover the situation as this needs to be a const.

Upvotes: 1

Views: 53

Answers (1)

heap underrun
heap underrun

Reputation: 2509

Just use # instead of = to separate the preprocessing symbol from its value when defining it via a command line. For example, this

CL /D THING#1234# somefile.c

will be converted to:

#define THING 1234#

Or, in case the THING macro must expand to a proper (quoted) string literal, use the following format (note that quotes must be escaped with \ on the command line to prevent being swallowed up):

CL /D THING#\"1234#\" somefile.c

This will be converted to:

#define THING "1234#"

Upvotes: 1

Related Questions