error: stray '\' in program in macro definition

前端 未结 1 437
既然无缘
既然无缘 2021-01-25 22:25

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:         


        
相关标签:
1条回答
  • 2021-01-25 23:14

    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.

    0 讨论(0)
提交回复
热议问题