I’m writing a c++ program and trying to pass a string value from the makefile to the code using the -DMACRO=VALUE format but it results in a compiler error that I don’t quite understand.
Here is the code
//#undef PROGRAM
//#define PROGRAM "xyzzy"
#include <cstdio>
int main ( int argc, char ** argv )
{
printf ( "%sn", PROGRAM );
return 0;
}
and here is the compilation
$ g++ -g -O2 -DPROGRAM="xyzzy" -c -o main.o main.cpp
main.cpp: In function ‘int main(int, char**)’:
<command-line>: error: ‘xyzzy’ was not declared in this scope
main.cpp:9:26: note: in expansion of macro ‘PROGRAM’
9 | printf ( "%sn", PROGRAM );
| ^~~~~~~
Uncommenting the first 2 lines produces the desired result – i.e. no errors and the program prints out the value of the define.
I’m using g++ (GCC) 11.4.0 on Cygwin/Windows 10.
Can anyone explain what is going on here?
Thanks
Malcolm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.