while trying to compile this program with scons, we faced this error
build/opt/zsim_harness.cpp:315:5: error: stray '\' in program
build/opt/zsim_harness.cpp:315:5: error: stray '#' in program
build/opt/zsim_harness.cpp: In function 'int main(int, char**)':
build/opt/zsim_harness.cpp:310:24: error: 'ZSIM_BUILDVERSION' was not declared in this scope
build/opt/zsim_harness.cpp:315:5: error: expected ')' before 'n'
build/opt/zsim_harness.cpp:315:5: error: 'ZSIM_BUILDVERSION' was not declared in this scope
The line that has this error is
info("Starting zsim, built %s (rev %s)", ZSIM_BUILDDATE, ZSIM_BUILDVERSION);
ZSIM_BUILDVERSION
is a macro which is defined in SConstruct
if os.path.exists(".git"):
env.Command(versionFile, allSrcs + [".git/index", "SConstruct"],
'echo "#define ZSIM_BUILDDATE \\""`date`\\""\\\\n#define ZSIM_BUILDVERSION \\""`python misc/gitver.py`\\""" >>' + versionFile)
else:
env.Command(versionFile, allSrcs + ["SConstruct"],
'echo "#define ZSIM_BUILDDATE \\""`date`\\""\\\\n#define ZSIM_BUILDVERSION \\""no git repo\\""" >>' + versionFile)
The scons version is 2.1.0 Any idea to fix that?
UPDATE:
It seems that scons will generate a file version.h which looks like
#define ZSIM_BUILDDATE "Sat Apr 19 11:07:38 CET 2014"\n#define ZSIM_BUILDVERSION "master:10:a8c417b:1fc 1+ 1- 6b4f4490"
The problem is that the Python code has too many backslashes in this line:
'echo "#define ZSIM_BUILDDATE \\""`date`\\""\\\\n#define ZSIM_BUILDVERSION \\""no git repo\\""" >>' + versionFile)
This is being parsed by one less program that the code expects, so it ends up that '\\n' ends up in the file as the two characters '\' and 'n', rather than being parsed into a single newline character. The code is trying to avoid Python inserting the newline, which would break the shell command, and instead pass the escape sequence to the shell for it to change to a newline. Python provides an easier way to do this in the form of raw strings:
r'echo "#define ZSIM_BUILDDATE \""`date`\""\\n#define ZSIM_BUILDVERSION \""no git repo\""" >>' + versionFile)
and when it's put like this, you can see the error more clearly: the shell sees the escape sequence '\', so it prints a single \, followed by an n, rather than the escape sequence '\n' which would cause it to put a newline in the file.
来源:https://stackoverflow.com/questions/23166975/error-stray-in-program-in-macro-definition