2012-05-23 25 views
18

したがって、thisthisの両方がかなり明確です。単純に合格する/p:DefineConstants="SYMBOL"どのように条件付きコンパイルシンボル(DefineConstants)をmsbuildに渡しますか

テストプロジェクトでさえ、私にとってはまったく機能しません。私は/ pを渡すことを期待しています:DefineConstants = "SYMBOL"は、csprojで定義された条件付きコンパイル定数を上書きします。ない場合は、しかし...

下記のリスト全コード:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace DefineConstants 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
#if DEV 
      Console.WriteLine("DEV"); 
#elif UAT 
      Console.WriteLine("UAT"); 
#else 
      Console.WriteLine("No environment provided"); 
#endif 
     } 
    } 
} 

.csprojファイルには、次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <PropertyGroup> 
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform> 
    <ProductVersion>8.0.30703</ProductVersion> 
    <SchemaVersion>2.0</SchemaVersion> 
    <ProjectGuid>{57A2E870-0547-475C-B0EB-66CF9A2FE417}</ProjectGuid> 
    <OutputType>Exe</OutputType> 
    <AppDesignerFolder>Properties</AppDesignerFolder> 
    <RootNamespace>DefineConstants</RootNamespace> 
    <AssemblyName>DefineConstants</AssemblyName> 
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> 
    <FileAlignment>512</FileAlignment> 
    </PropertyGroup> 
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> 
    <PlatformTarget>x86</PlatformTarget> 
    <DebugSymbols>true</DebugSymbols> 
    <DebugType>full</DebugType> 
    <Optimize>false</Optimize> 
    <OutputPath>bin\Debug\</OutputPath> 
    <DefineConstants>TRACE;DEBUG</DefineConstants> 
    <ErrorReport>prompt</ErrorReport> 
    <WarningLevel>4</WarningLevel> 
    </PropertyGroup> 
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> 
    <PlatformTarget>x86</PlatformTarget> 
    <DebugType>pdbonly</DebugType> 
    <Optimize>true</Optimize> 
    <OutputPath>bin\Release\</OutputPath> 
    <DefineConstants>TRACE</DefineConstants> 
    <ErrorReport>prompt</ErrorReport> 
    <WarningLevel>4</WarningLevel> 
    </PropertyGroup> 
    <ItemGroup> 
    <Reference Include="System" /> 
    <Reference Include="System.Core" /> 
    <Reference Include="System.Xml.Linq" /> 
    <Reference Include="System.Data.DataSetExtensions" /> 
    <Reference Include="System.Data" /> 
    <Reference Include="System.Xml" /> 
    </ItemGroup> 
    <ItemGroup> 
    <Compile Include="Program.cs" /> 
    <Compile Include="Properties\AssemblyInfo.cs" /> 
    </ItemGroup> 
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 
    <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
     Other similar extension points exist, see Microsoft.Common.targets. 
    <Target Name="BeforeBuild"> 
    </Target> 
    <Target Name="AfterBuild"> 
    </Target> 
    --> 
</Project> 

使用して構築された:プログラムを実行

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild DefineConstants.sln /p:DefineConstants="DEV;DEBUG" /p:Configuration="Debug" /p:Platform="x86" 

を示しています。

No environment provided 

ヘルプ!

+0

解決策を右クリック - > Clean Solution - >ビルドコマンドを再試行してください。役立つかもしれません。 – Alex

+0

あなたはslnではなくcsprojファイルでmsbuildを使ってみましたか? – KazR

答えて

26

使用すると、DefineConstantsは間違いなく動作します。あなたが何か間違っていることを意味します。私の推測では、何も定義せずにプロジェクトを構築した後、再びビルドしています。 MSBuildはそのプロジェクトが既にビルドされているのを確認し、ビルドは再開しませんが、出力ファイルをコピーするだけです。私たちは確認することができますので、あなたがのMSBuildの出力を投稿する必要がありますが、参考のために私は、ここでは結果であり、必要なスイッチを使用してプロジェクトをコンパイル(フルMSBuildの出力は省略):

> msbuild ConsoleApplication1.sln /p:DefineConstants="DEV" /t:Rebuild 
.... 
Building solution configuration "Debug|x86". 
Project ... is building ConsoleApplication1.csproj" (Rebuild target(s)). 
... 
> ConsoleApplication1\bin\Debug\DefineConstants.exe 
DEV 
> msbuild ConsoleApplication1.sln /p:DefineConstants="UAT" /t:Rebuild 
... 
> ConsoleApplication1\bin\Debug\DefineConstants.exe 
UAT 
> msbuild ConsoleApplication1.sln /t:Rebuild 
... 
> ConsoleApplication1\bin\Debug\DefineConstants.exe 
No environment provided 
+5

/t:実際に再構築します。いい男。 –

+0

DefineConstantsを動作させるためにRebuildが必要であることが文書化されていますか?これは暗黙の常識ですか?したがって、DefineConstantsを使用して増分ビルドを行う方法はありませんか? – JohnZaj

+0

@jJack * incremental *の意味に依存します:最初にA.csを編集し、次にビルドしてB.csを編集してもう一度ビルドすると、うまくいくはずです。私は、B.csをビルドする前にDefineConstantsを変更してもA.csに影響することは100%確信していませんが、もちろん試してみることができます。 – stijn

0

あなたが合格されていないようです条件付きコンパイルシンボル。そのため、あなたは次のように出力を得ています:環境は提供されていません。プロジェクトプロパティに移動し、ビルドタブ(アプリケーションの下の左側)をクリックするだけです。条件付きコンパイルシンボルを求めるボックスが1つ見つかります。あなたの希望する出力ごとに定数を書いてください。例えば、あなたの例では、条件付きコンパイルボックスに "DEV" write DEVを印刷し、プロジェクトを再構築します。私はそれが動作すると確信しています。

注:あなたのコードに従ってシンボルを渡したい場合、それは印刷されます:環境は選択されていません。

+0

古い質問に答えるときは慎重にしてください。これは5歳以上で、受け入れられた答えがあります。これは現在、余分なものを何も追加しません。 - [レビュー](https://stackoverflow.com/review) – Takarii

+0

こんにちは、その真実。しかし、なぜ私はこのansを置く、それは同じ質問を探して他の人を助けるかもしれない。 –

関連する問題