问题描述
尝试使用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:在函数'int main(int,char **)'中:
build / opt / zsim_harness.cpp: 310:24:错误:'ZSIM_BUILDVERSION'未在此范围内声明
build / opt / zsim_harness.cpp:315:5:error:expected')'before'n'
build / opt / zsim_harness .cpp:315:5:错误:'ZSIM_BUILDVERSION'未在此范围内声明
有这个错误是
info(Starting zsim,built%s(rev%s),ZSIM_BUILDDATE,ZSIM_BUILDVERSION);
ZSIM_BUILDVERSION
SConstruct
如果os.path.exists(。git):
env.Command(versionFile,allSrcs + [.git / index,SConstruct],
'echo#define ZSIM_BUILDDATE \\date` \\\\\\\\
#define ZSIM_BUILDVERSION \\`python misc / gitver.py`\\>>'+ versionFile)
else:
env.Command(versionFile,allSrcs + [SConstruct ],
'echo#define ZSIM_BUILDDATE \\`````````````````git repo\\ \\\>>''+ versionFile)
有任何想法可以解决这个问题吗?
UPDATE:
看起来scons会生成一个文件version.h看起来像
#define ZSIM_BUILDDATESat Apr 19 11:07:38 CET 2014\\\
#define ZSIM_BUILDVERSIONmaster:10:a8c417b:1fc 1+ 1- 6b4f4490
问题是Python代码在这一行中有太多反斜杠:
'echo#define ZSIM_BUILDDATE \\ \\\`date`\\\\\\\\\\ #include ZSIM_BUILDVERSION \\no git repo\\>>'+ versionFile)
这是由代码预期的一个少的程序解析, \\\
'以两个字符'\'和'n'结尾在文件中,而不是解析为单个换行符。代码试图避免Python插入换行符,这将破坏shell命令,而是将转义序列传递给shell以更改为换行符。 Python提供了一种更简单的方式,以原始字符串的形式这样做:
r'echo#define ZSIM_BUILDDATE \ `date`\\\\\
#define ZSIM_BUILDVERSION \no git repo\>>'+ versionFile)
,当它这样放置时,你可以更清楚地看到错误:shell看到转义序列'\',所以它打印一个单独的\,一个n,而不是转义序列'\\\
',这将导致它在文件中放一个换行符。
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.0Any 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.
这篇关于错误:stray'\'在程序中的宏定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!