msbuild, defining Conditional Compilation Symbols

前端 未结 4 2069
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 02:59

I\'m possibly just blind, but is there a command line to specify conditional compilation symbols in MSBUILD?

I currently have this Line in my buildscript:

         


        
相关标签:
4条回答
  • 2020-11-29 03:04

    What is said in the answers is valid for C# code, and also for ASP.NET "codebehind" C# code. For ASP.NET web projects, if you want to do conditional compilation in the ASPX pages as well, it works a bit differently to conditionally render HTML on the page (note I've removed MasterPageFile="..." AutoEventWireup="true" CodeBehind="..." Inherits="..." which you usually have in the <%@ ... %> declaration as well):

    <%@ Page Title="MyPage" Language="C#" CompilerOptions="/d:DebugSym1;DebugSym2" %>
    
    <% #if DebugSym1 %>         
        <h4>Section1</h4>
    <% #else %>
        <h4>(Section 1 skipped)</h4>
    <% #endif %>
    
    <% #if DebugSym2 %>         
        <h4>Section2</h4>
    <% #else %>
        <h4>(Section 2 skipped)</h4>
    <% #endif %>
    

    If you remove DebugSym1 or DebugSym2 from the CompilerOptions, then the #else part of the relevant #if statement is rendered.

    I thought this was worth mentioning for completeness of this topic and can save you time. More you can find in this article, if you're interested.

    0 讨论(0)
  • 2020-11-29 03:12

    /p:DefineConstants is an all or nothing deal.

    If you just want to turn off trace symbol, you can't just do it with: msbuild /p:DefineTrace=false

    You have to define something to override all the symbols already defined: msbuild /p:DefineConstants="RANDOM-SYMBOL"

    Thanks Michael Stum point this hidden rule out I have also wrote a blog about it --- dead link

    0 讨论(0)
  • 2020-11-29 03:19

    I had to use a space instead of a semicolon a la this post by Björn Lasar: http://www.linqinpark.net/2009/01/13/MSBuildWithMultipleDefineConstants.aspx

    Update: the blog has disappeared; retrieved via Internet Archive:

    Recently I had to use MSBuild directly to automate some builds. I also had to configure some preprocessor defines based upon a configuration. This is usually done by an Argument like this

    "/p:DefineConstants=MY_PREPROC_FLAG"
    

    Nothing special here since there are enough comments on the web about that. Today I needed one Flag more and I used the commandline syntax similar to how I knew it from the IDE:

    "/p:DefineConstants=MY_PREPROC_FLAG;YET_ANOTHER_FLAG"
    

    but this one didn't work.

    So the point is that if you want to support multiple defines to a project by commandline you'll have to separate them by simple spaces...

    "/p:DefineConstants=MY_PREPROC_FLAG YET_ANOTHER_FLAG" 
    

    and it will be added to the (semicolon-separated) Defines from the IDE. Good to know I think...

    0 讨论(0)
  • 2020-11-29 03:27

    Have you seen this? (most info is in the penultimate post)

    /p:DefineConstants="MYSYMBOL1;MYSYMBOL2"
    
    0 讨论(0)
提交回复
热议问题