Author Topic: preprocessor macro expansion  (Read 2196 times)

Offline r.stiltskin

  • Multiple posting newcomer
  • *
  • Posts: 48
preprocessor macro expansion
« on: March 27, 2015, 07:57:14 am »
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;
}
in a makefile, or at the command line by running
Code
g++ -o dummy -DVER=\"0.1\" main.cpp
or
Code
export VAR=0.1
g++ -o dummy -DVER=\"$VAR\" main.cpp
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
and various other combinations but nothing seems to work.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: preprocessor macro expansion
« Reply #1 on: March 27, 2015, 10:44:25 am »
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.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline r.stiltskin

  • Multiple posting newcomer
  • *
  • Posts: 48
Re: preprocessor macro expansion
« Reply #2 on: March 27, 2015, 05:25:56 pm »
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.