error: stray '\' in program in macro definition

↘锁芯ラ 提交于 2019-12-02 17:54:36

问题


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"

回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!