尝试编译时 this program使用 scons,我们遇到了这个错误
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
出现这个错误的行是
info("Starting zsim, built %s (rev %s)", ZSIM_BUILDDATE, ZSIM_BUILDVERSION);
ZSIM_BUILDVERSION
是在 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)
scons版本为2.1.0 有解决办法吗?
更新:
好像scons会生成一个文件version.h,这个样子
#define ZSIM_BUILDDATE "Sat Apr 19 11:07:38 CET 2014"\n#define ZSIM_BUILDVERSION "master:10:a8c417b:1fc 1+ 1- 6b4f4490"
最佳答案
问题是 Python 代码在这一行中有太多的反斜杠:
'echo "#define ZSIM_BUILDDATE \\""`date`\\""\\\\n#define ZSIM_BUILDVERSION \\""no git repo\\""" >>' + versionFile)
这是由代码预期的少一个程序解析的,所以它最终导致 '\\n' 在文件中作为两个字符 '\' 和 'n' 结束,而不是被解析为单个换行符。该代码试图避免 Python 插入换行符,这会破坏 shell 命令,而是将转义序列传递给 shell 以便它更改为换行符。 Python 以原始字符串的形式提供了一种更简单的方法来执行此操作:
r'echo "#define ZSIM_BUILDDATE \""`date`\""\\n#define ZSIM_BUILDVERSION \""no git repo\""" >>' + versionFile)
当它像这样放置时,您可以更清楚地看到错误:shell 看到转义序列 '\',因此它打印一个\,后跟一个 n,而不是转义序列 '\n'会导致它在文件中放置一个换行符。
关于python - 错误 : stray '\' in program in macro definition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23166975/