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

时间:2010-01-06 22:18:25

标签: command-line msbuild macros c-preprocessor

我已经阅读了很多关于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年看到这个)在其内容中有一些默认设置,这阻止了我的命令行参数的应用,但我没有找到证据支持这个假设。 / p>

关于如何让它发挥作用的任何想法?为什么在FSM的名义下他们没有坚持好'-D THING_TO_BE_DEFINED?

4 个答案:

答案 0 :(得分:8)

如果在命令行上调用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撰写了一篇关于此相关内容的博客文章。

答案 1 :(得分:7)

如果你想定义TRACE&amp; DEBUG常量应该可以工作:

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

答案 2 :(得分:3)

以下是需要修改vcxproj以使/ p工作。

放 &LT;的 DefineConstants &GT;&LT; /的 DefineConstants &GT;

&lt; PropertyGroup Label = Globals&gt;

下的

&LT;的 PreprocessorDefinitions &GT; $(的 DefineConstants ); WIN32; _DEBUG; _CONSOLE; UNIT_TEST_SIM;%(PreprocessorDefinitions)

这样,MSBuild将知道对于预处理器,它需要使用来自Globals PropertyGroup的DefineConstants中的值,除非从命令行通过/ p提供:DefineConstants =“MY_DEFINE”

答案 3 :(得分:2)

为了完整性,这是我在VB.NET中需要THING_TO_BE_DEFINED="VALUE WANTED"和批处理文件中的msbuild版本3.5.30729.1时发现的工作:

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

(当然全部在一行上)