User forums > Using Code::Blocks
preprocessor macro expansion
(1/1)
r.stiltskin:
Sorry if this is too dumb.
I can easily compile this code
--- Code: ---#include <iostream>
#define PROG_ID "dummy v" VER
using namespace std;
int main()
{
cout << PROG_ID << endl;
return 0;
}
--- End code ---
in a makefile, or at the command line by running
--- Code: ---g++ -o dummy -DVER=\"0.1\" main.cpp
--- End code ---
or
--- Code: ---export VAR=0.1
g++ -o dummy -DVER=\"$VAR\" main.cpp
--- End code ---
but I can't work out how to do it in CB.
I tried putting
-DVER=\"0.1\"
in the project's build options/compilersettings/#defines
I also tried under Project/ build options/custom variables defining VER=0.1 and changing line 2 of the code to
--- Code: ---#define PROG_ID "dummy v" $VER
--- End code ---
and various other combinations but nothing seems to work.
oBFusCATed:
Don't prepend -D when you put something in the defines section.
Also you'll probably need to put more quotes until the string is passed correctly.
There are several levels of escaping that happens.
But if you don't need to pass real strings it is better to pass VER=1.0 and then in the code use a stringify like macro expansion to turn it to a string.
This way you won't depend on the expansions that happen in different build systems.
r.stiltskin:
Thanks. I see your point re stringify, but I'm bringing someone else's existing project into CB and trying to minimize the changes.
But your "several levels" comment gave the solution. It turns out that I didn't need more quotes but needed more backslashes. So putting
VER=\\"0.1\\"
in #defines did the trick.
Navigation
[0] Message Index
Go to full version