0

Is there a way to expand a macro in C++ and re-create output file. For example:-

cat tmp.cpp 
#include<iostream>
using namespace std;

int main() {
    cout<<"Macro = "<<VALUE<<endl;
}

with -DVALUE=10

I want a file to show (notice VALUE replaced by 10)

#include<iostream>
using namespace std;

int main() {
    cout<<"Macro = "<<10<<endl;
}
user1918858
  • 1,202
  • 1
  • 20
  • 29
  • 2
    The preprocessor can often be run separately. Either as a separate program, or by telling the compiler frontend program to stop after preprocessing. The problem with that is, of course, that the preprocessed output will contain the included header files and possible `#line` compiler directives. – Some programmer dude Mar 07 '17 at 21:49
  • preprocessor -E flag to the gcc should give you the desired output `g++ tmp.cpp -DVALUE=10 -E | tail` – Mohit Mar 07 '17 at 21:52
  • What real problem are you trying to solve. No, not the one the question is asking, but the problem to which you believe the solution is what the question is asking. – Sam Varshavchik Mar 07 '17 at 21:57
  • @Mohit - I had tried that method but unfortunately, it removes the headers and shows a lot of unnecessary debug messages extern __attribute__ ((__visibility__("default"))) ostream clog; extern __attribute__ ((__visibility__("default"))) wostream wclog; – user1918858 Mar 07 '17 at 23:29
  • http://stackoverflow.com/users/3943312/sam-varshavchik We have a product in production with namespace "company::Producer::..." which we open sourced and changed the namespace "opensource::Producer::..." Now, we develop only on the open source branch and I want to create a wrapper so that we can use the open source code in production by adding a macro -Dcompany=opensource - this works but we also need to provide headers for which I was thinking if I can recreated the headers with namespace "opensource::Producer::..." – user1918858 Mar 08 '17 at 02:54
  • http://stackoverflow.com/questions/8713336/run-preprocessor-only-but-with-only-for-certain-statements – Mohit Mar 08 '17 at 16:03

0 Answers0