0

I'm trying to use the C preprocessor for some text mangling. It seems to be actively trying to thwart me, however.

For test purposes, I have Test.xml.pre:

<document>
  <meta>This is a test.</meta>
  SETTINGS
  <output format="FORMAT" mode="MODE"/>
</document>

and then I have Setup.cpp:

#define FORMAT fmt
#define MODE XL

#define SETTINGS <settings><!-- None yet --></settings>

I was hoping that I could somehow run the preprocessor and end up with well-formed output. I saw this question, which makes it look quite easy to use the preprocessor. But when I do this:

cpp -E -include Setup.cpp Test.xml.pre -o Test.xml

then I get this:

# 1 "Test.xml.pre"
# 1 "<command-line>"
# 1 "./Setup.cpp" 1
# 1 "<command-line>" 2
# 1 "Test.xml.pre"
<document>
  <meta>This is a test.</meta>
  <settings><!-- None yet --></settings>
  <output format="FORMAT" mode="MODE"/>
</document>

Try as I might, I cannot get rid of the hash lines at the top. I'm also not sure why FORMAT and MODE as not expanding. (SETTINGS seems to expand just fine.)

What am I doing wrong??

PS. In case it matters,

cpp (SUSE Linux) 4.8.1 20130909 [gcc-4_8-branch revision 202388]
Community
  • 1
  • 1
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220

1 Answers1

1

To get rid of the line definitions, use the -P command line argument.

Identifiers inside string literals are not expanded, so you cannot use FORMAT and MODE the way you have done. However, this works:

<document>
  <meta>This is a test.</meta>
  SETTINGS
  <output format=FORMAT mode=MODE/>
</document>

with

#define FORMAT "fmt"
#define MODE "XL"
#define SETTINGS <settings><!-- None yet --></settings>

to get

$ cpp -P -E -include Setup.cpp Test.xml.pre -o Test.xml
$ cat Test.xml
<document>
  <meta>This is a test.</meta>
  <settings><!-- None yet --></settings>
  <output format="fmt" mode="XL"/>
</document>
jlahd
  • 6,257
  • 1
  • 15
  • 21