英文:
msbuild - Why do I get error MSB4025 when using output redirection in my VS NMake Build Command Line?
问题
我使用Visual Studio的makefile项目,并在vcxproj文件的NMakeBuildCommandLine属性中有单独的构建命令。当其中一个命令使用输出重定向时,如下所示:
echo "不需要的输出" > nul 2>&1
当调用msbuild时,我会收到构建错误:
all.vcxproj(53,30): error MSB4025:
项目文件无法加载。在解析EntityName时发生错误。第53行,第33位。
或者
Die Projektdatei konnte nicht geladen werden. Beim Analysieren von 'EntityName' ist ein错误 aufgetreten. Zeile 53, Position 33.
为什么?
我自己找到了解决方案,并想在这里提供它。
英文:
I use a Visual Studio makefile project and have individual build commands in the NMakeBuildCommandLine property of the vcxproj file. <br>
When one of these commands uses output redirection like this: <br>
echo "unwanted output" >nul 2>&1
I get a build error when calling msbuild: <br>
all.vcxproj(53,30): error MSB4025:
<br>
The project file could not be loaded. An error occurred while parsing EntityName. Line 53, position 33.
<br>
or <br>
Die Projektdatei konnte nicht geladen werden. Beim Analysieren von 'EntityName' ist ein Fehler aufgetreten. Zeile 53, Position 33.
<br>
Why?
I found the solution by myself and want to provide it here.
答案1
得分: 1
这个错误只会发生在你直接编辑 vcxproj 文件中的 NMakeBuildCommandLine
时。<br>
你也可以在其他命令属性中遇到相同的错误。
原因是 >
和 &
是无效的特殊字符。<br>
根据 MSBuild 帮助 的说明,特殊字符需要进行转义。<br>
然而,在这个帮助中没有列出 >
和 &
,尽管它们在 XML 元素中是无效的,而 vcxproj 是 XML。
MSBuild 帮助中说道 <br>
> 要转义特殊字符,请使用语法 %<xx>,其中 <xx> 代表字符的 ASCII 十六进制值。
这意味着要将 <br>
echo "unwanted output" >nul 2>&1
替换为
echo "unwanted output" %3enul 2%3e%261
通过尝试,我发现以下变体也有效:
echo "unwanted output" &#62;nul 2&#62;&#38;1
echo "unwanted output" &gt;nul 2&gt;&amp;1
有趣的是,只有最后两个变体在项目属性中正确显示为 >nul 2>&1
。<br>
在直接编辑项目属性 > NMake > Build Command Line 时,如果输入 >nul 2>&1
,Visual Studio 会将 &gt;nul 2&gt;&amp;1
写入 vcxproj。
由于最后一个变体比十六进制语法更可读,我也更喜欢这种方式。
我在 Visual Studio 17.2.5 中进行了测试。
这里 也有一个类似的答案,但它既没有提到字符 >
(用于输出重定向但不包含在 MSBuild 帮助 中),也没有提到 NMakeBuildCommandLine(这是我遇到的问题)。<br> 因此,我决定创建这个新问题和答案,以帮助其他遇到类似问题的人。
英文:
This error can only happen, if you directly edit the NMakeBuildCommandLine
within the vcxproj file. <br>
You can get the same error with other command properties too.
The reason is, that >
and &
are invalid special characters. <br>
As stated in the MSBuild help, special characters need to be escaped. <br>
However in this help >
and &
are not listed, although they are invalid in XML elements and a vcxproj is xml.
The MSBuild help says <br>
> To escape a special character, use the syntax %<xx>, where <xx> represents the ASCII hexadecimal value of the character.
which means to replace <br>
echo "unwanted output" >nul 2>&1
with
echo "unwanted output" %3enul 2%3e%261
By try I found out, that the following variants do also work:
echo "unwanted output" &#62;nul 2&#62;&#38;1
echo "unwanted output" &gt;nul 2&gt;&amp;1
Interestingly, only the last 2 variants are afterwards displayed correctly in the project properties > NMake > Build Command Line as >nul 2>&1
. <br>
When editing the project properties > NMake > Build Command Line directly, and you enter >nul 2>&1
, VS writes &gt;nul 2&gt;&amp;1
to the vcxproj.
And since the last variant is more readable than the hexadecimal syntax, I would prefer this too.
I tested with Visual Studio 17.2.5.
There was also a similar answer here, but it does neither mention the character >
(which is used for output redirection but not contained in the MSBuild help) nor does it mention NMakeBuildCommandLine (that's the problem I was coming from). <br> So I decided to make this new question & answer to help others with a similar problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论