MSBuild.exe 不接受 /p:DefineConstants 和 /p:PreprocessorDefinitions

发布于 2024-08-17 04:43:17 字数 928 浏览 4 评论 0原文

我已经阅读了 Stack Overflow 上的很多文章,这些文章回答了“如何从 MSBuild 命令行将预处理器定义传递给编译器”这一问题,并且它们都以以下一些变化进行了回应:

MSBuild.exe /p:DefineConstants=THING_TO_BE_DEFINED

我已经尝试了我所尝试的每一种变化可以想出:

MSBuild.exe "/p:DefineConstants=THING_TO_BE_DEFINED"
MSBuild.exe /p:DefineConstants="THING_TO_BE_DEFINED"
MSBuild.exe "/p:DefineConstants=THING_TO_BE_DEFINED=1"
MSBuild.exe /p:DefineConstants="THING_TO_BE_DEFINED=1"

……以及其他几十个。我还以类似的方式尝试过重写 PreprocessorDefinitions。所有这些都触发​​了下面的#error:

#include "stdafx.h"

#if !defined(THING_TO_BE_DEFINED)
#error "THING_TO_BE_DEFINED is not defined"
#endif

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

我一直在使用上面的简单命令行应用程序以及我这里的一个巨大的游戏项目来尝试此操作。我只能猜测 Visual Studio(我在 2005 年和 2008 年看到了这一点)在其内部深处设置了一些默认设置,阻止了我的命令行参数的应用,但我没有发现任何证据来支持这个假设。

关于如何让它发挥作用有什么想法吗?为什么以 FSM 的名义他们不坚持使用好的 ol'-D THING_TO_BE_DEFINED 呢?

I've been through quite a number of articles on Stack Overflow that answered the question "How do I pass preprocessor definitions to the compiler from the MSBuild command line," and they all responded with some variation of:

MSBuild.exe /p:DefineConstants=THING_TO_BE_DEFINED

I have tried every variation that I could come up with:

MSBuild.exe "/p:DefineConstants=THING_TO_BE_DEFINED"
MSBuild.exe /p:DefineConstants="THING_TO_BE_DEFINED"
MSBuild.exe "/p:DefineConstants=THING_TO_BE_DEFINED=1"
MSBuild.exe /p:DefineConstants="THING_TO_BE_DEFINED=1"

...and dozens of others. I've also flirted with overriding PreprocessorDefinitions in similar ways. All of them triggered the #error below:

#include "stdafx.h"

#if !defined(THING_TO_BE_DEFINED)
#error "THING_TO_BE_DEFINED is not defined"
#endif

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

I've been trying this with the simple command-line application above, as well as with a huge game project that I have here. I can only guess that Visual Studio (I'm seeing this with 2005 and 2008) has some default set deep in its bowels that is preventing my command line argument from being applied, but I've found no evidence to support this hypothesis.

Any ideas on how I can get this to work? Why in the name of FSM didn't they stick with good ol' -D THING_TO_BE_DEFINED?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果在命令行上调用 MSBuild,则无法指定 DefineConstants 的值。但如果您正在构建 .csproj 或另一个 MSBuild 脚本,则可以指定它。如果您创建一个 msbuild 文件来“替换”您的解决方案文件,那么您可以在构建项目时使用该文件并为其指定值。例如:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <!-- Default value here -->
    <DefineConstants Condition=" '$(DefineConstants)'==''" >DEBUG;TRACE</DefineConstants>
  </PropertyGroup>

  <ItemGroup>
    <Projects Include="one.csproj" />
    <Projects Include="two.csproj" />
  </ItemGroup>

  <Target Name="Build">
    <MSBuild Projects="@(Projects)"
                 Properties="DefineConstants=$(DefineConstants)"/>
  </Target>
</Project>

然后您可以使用 msbuild.exe buid.proj /p:DefineConstants="YourValue;Debug;Trace"

注意命令行中引号的用法。

我不久前在 http://sedodream 上写了一篇关于与此相关的内容的博客文章。 com/2008/05/07/MSBuildBuildingTheSameProjectMultipleTimes.aspx

If you are calling MSBuild on the command line you cannot specify the value for DefineConstants. But if you are building a .csproj, or another MSBuild script, then you can specify it. If you create a msbuild file to "replace" your solution file then you can use that an specify the value for that when you build your projects. For example:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <!-- Default value here -->
    <DefineConstants Condition=" '$(DefineConstants)'==''" >DEBUG;TRACE</DefineConstants>
  </PropertyGroup>

  <ItemGroup>
    <Projects Include="one.csproj" />
    <Projects Include="two.csproj" />
  </ItemGroup>

  <Target Name="Build">
    <MSBuild Projects="@(Projects)"
                 Properties="DefineConstants=$(DefineConstants)"/>
  </Target>
</Project>

Then you can use msbuild.exe buid.proj /p:DefineConstants="YourValue;Debug;Trace"

Note the usage of the quotes on the command line.

I have written a blog post a while back about something related to this at http://sedodream.com/2008/05/07/MSBuildBuildingTheSameProjectMultipleTimes.aspx.

唔猫 2024-08-24 04:43:17

如果你想定义TRACE & DEBUG 常量这应该有效:

msbuild mysln.sln /t:Rebuild /p:Configuration=Release /p:DefineConstants="DEBUG;TRACE"

If you want to define TRACE & DEBUG Constants this should work:

msbuild mysln.sln /t:Rebuild /p:Configuration=Release /p:DefineConstants="DEBUG;TRACE"
爱要勇敢去追 2024-08-24 04:43:17

为了使 /p 工作,需要对 vcxproj 进行以下修改。


<定义常量>< /定义常量>

在 <PropertyGroup Label=Globals > 下

<PreprocessorDefinitions>$(DefineConstants);WIN32;_DEBUG;_CONSOLE;UNIT_TEST_SIM;%(PreprocessorDefinitions)

这样 MSBuild 就会知道,对于预处理器,它需要使用来自全局 PropertyGroup 的 DefineConstants 的值,除非通过 /p:DefineConstants="MY_DEFINE" 从命令行提供

The below are needed modification to the vcxproj for the /p to work.

put
<DefineConstants>< /DefineConstants>

under the <PropertyGroup Label=Globals >

<PreprocessorDefinitions>$(DefineConstants);WIN32;_DEBUG;_CONSOLE;UNIT_TEST_SIM;%(PreprocessorDefinitions)

This way MSBuild will know that for the preprocessor it needs to use the values from the DefineConstants which come from the Globals PropertyGroup unless provided from the command line by the /p:DefineConstants="MY_DEFINE"

暗恋未遂 2024-08-24 04:43:17

为了完整起见,当我想要 THING_TO_BE_DEFINED="VALUE WANTED"(用于 VB.NET)和 msbuild 版本 3.5.30729.1(在批处理文件中)时,我发现这是有效的:

@msbuild /t:Rebuild /p:Configuration=Release;Platform="Any CPU";
DefineConstants="THING_TO_BE_DEFINED=\"VALUE WANTED\"" mysln.sln

(全部在一个 批处理文件中)当然行)

For completeness, this is what I found worked when I wanted THING_TO_BE_DEFINED="VALUE WANTED", for VB.NET, and msbuild version 3.5.30729.1, in a batch file:

@msbuild /t:Rebuild /p:Configuration=Release;Platform="Any CPU";
DefineConstants="THING_TO_BE_DEFINED=\"VALUE WANTED\"" mysln.sln

(all on one line of course)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文