msbuild – Why do I get error MSB4025 when using output redirection in my VS NMake Build Command Line?

huangapple go评论58阅读模式
英文:

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 &quot;unwanted output&quot;  &gt;nul 2&gt;&amp;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 &#39;EntityName&#39; 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>
你也可以在其他命令属性中遇到相同的错误。

原因是 &gt;&amp; 是无效的特殊字符。<br>
根据 MSBuild 帮助 的说明,特殊字符需要进行转义。<br>
然而,在这个帮助中没有列出 &gt;&amp;,尽管它们在 XML 元素中是无效的,而 vcxproj 是 XML。

MSBuild 帮助中说道 <br>
> 要转义特殊字符,请使用语法 %&lt;xx&gt;,其中 &lt;xx&gt; 代表字符的 ASCII 十六进制值。

这意味着要将 <br>

echo &quot;unwanted output&quot;  &gt;nul 2&gt;&amp;1

替换为

echo &quot;unwanted output&quot;  %3enul 2%3e%261

通过尝试,我发现以下变体也有效:

echo &quot;unwanted output&quot;  &amp;#62;nul 2&amp;#62;&amp;#38;1
echo &quot;unwanted output&quot;  &amp;gt;nul 2&amp;gt;&amp;amp;1

有趣的是,只有最后两个变体在项目属性中正确显示为 &gt;nul 2&gt;&amp;1。<br>
在直接编辑项目属性 > NMake > Build Command Line 时,如果输入 &gt;nul 2&gt;&amp;1,Visual Studio 会将 &amp;gt;nul 2&amp;gt;&amp;amp;1 写入 vcxproj。

由于最后一个变体比十六进制语法更可读,我也更喜欢这种方式。

我在 Visual Studio 17.2.5 中进行了测试。


这里 也有一个类似的答案,但它既没有提到字符 &gt;(用于输出重定向但不包含在 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 &gt; and &amp; are invalid special characters. <br>
As stated in the MSBuild help, special characters need to be escaped. <br>
However in this help &gt; and &amp; 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 %&lt;xx&gt;, where &lt;xx&gt; represents the ASCII hexadecimal value of the character.

which means to replace <br>

echo &quot;unwanted output&quot;  &gt;nul 2&gt;&amp;1

with

echo &quot;unwanted output&quot;  %3enul 2%3e%261

By try I found out, that the following variants do also work:

echo &quot;unwanted output&quot;  &amp;#62;nul 2&amp;#62;&amp;#38;1
echo &quot;unwanted output&quot;  &amp;gt;nul 2&amp;gt;&amp;amp;1

Interestingly, only the last 2 variants are afterwards displayed correctly in the project properties > NMake > Build Command Line as &gt;nul 2&gt;&amp;1. <br>
When editing the project properties > NMake > Build Command Line directly, and you enter &gt;nul 2&gt;&amp;1, VS writes &amp;gt;nul 2&amp;gt;&amp;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 &gt; (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.

huangapple
  • 本文由 发表于 2023年4月19日 16:27:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定