How to use custom preprocessor directives in .Net Core

前端 未结 2 1692
不思量自难忘°
不思量自难忘° 2020-12-11 00:58

I am trying to use a preprocessor directive in .Net core, but I can\'t determine the correct way to get the directive to be set:

static void Main(string[] ar         


        
相关标签:
2条回答
  • 2020-12-11 01:34

    The thing you need to set is /p:DefineConstants=MAC note this will override constants set in the project like DEBUG or TRACE that may be set so the full version you would likely use would be

    for a debug build

    dotnet msbuild /p:DefineConstants=TRACE;DEBUG;NETCOREAPP1_1;MAC /p:Configuration=Debug
    

    and for a release build

    dotnet msbuild /p:DefineConstants=TRACE;NETCOREAPP1_1;MAC /p:Configuration=Release
    

    An easier solution would create a configuration called Mac and in your csproj have

      <PropertyGroup Condition="'$(Configuration)'=='Mac'">
        <DefineConstants>TRACE;NETCOREAPP1_1;MAC</DefineConstants>
      </PropertyGroup>
    

    Then from the command line you just need to do

    dotnet msbuild /p:Configuration=Mac
    
    0 讨论(0)
  • 2020-12-11 01:46

    If you want custom configuration switches that do not impact other settings ("Configurations" like Debug/Release), you can define any other property and use it in your build.

    E.g. for dotnet build /p:IsMac=true you could add the following to your csproj file (not that run might not pass the property correctly though IsMac=true dotnet run will work after a clean):

    <PropertyGroup>
      <DefineConstants Condition=" '$(IsMac)' == 'true' ">$(DefineConstants);MAC</DefineConstants>
    </PropertyGroup>
    

    If you want to go further and automatically detect if you are building on a mac, you can use msbuild property functions to evaluate which OS you are building on. Not that this currently only works for the .net core variant of msbuild (dotnet msbuild). See this PR for details on the support.

    <PropertyGroup>
      <IsMac>$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::get_OSX())))</IsMac>
      <DefineConstants Condition=" '$(IsMac)' == 'true' ">$(DefineConstants);MAC</DefineConstants>
    </PropertyGroup>
    
    0 讨论(0)
提交回复
热议问题