Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio conditional compilation for os

I know that there is a way to conditionally compile for target frameworks eg #if net461 ....#elif .... But is there a way to conditionally compile for specific os Like target _os_MAC or target_os_win

If there is can someone guide me to the documentations or tutorials to how to implement it?

Part 2: Also , is there a way to create a custom tag so that I do not have to change every tag whenever there is a change to the new target os or framework .eg from net461 to net471

like image 945
Fishfish Avatar asked Apr 19 '18 01:04

Fishfish


2 Answers

It's an old question, but if someone comes here now, there's a better option.

You do not need to have different configurations and select manually which configuration to use.

You can use System.Runtime.InteropServices.RuntimeInformation. https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.runtimeinformation?view=netframework-4.8

There's a good manual here: https://blog.magnusmontin.net/2018/11/05/platform-conditional-compilation-in-net-core/ Minimal info from the link: Change your .csproj file

<PropertyGroup> 
 <OutputType>Exe</OutputType> 
 <TargetFramework>netcoreapp2.0</TargetFramework> 
 <IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows> 
 <IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux> 
</PropertyGroup>
<PropertyGroup Condition="'$(IsWindows)'=='true'">
 <DefineConstants>Windows</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsLinux)'=='true'">
 <DefineConstants>Linux</DefineConstants>
</PropertyGroup>

This will conditionally define constants. Which you later use like so:

#if Linux
    Console.WriteLine("Built on Linux!"); 
#elif Windows
    Console.WriteLine("Built in Windows!"); 
#endif
like image 200
Erndob Avatar answered Oct 17 '22 06:10

Erndob


This answer assumes you're asking about custom pre-processor symbols (that is how I have interpreted it - do correct me if I am wrong.

You could use a custom build configuration:

Start by going into the build Configuration Manager..

Configuration Manager

Next, create a new build configuration. You can copy the configuration from an existing one:

Create new configuration

Then, right click on your Project and go to Properties. Under the build tab, define a conditional compilation symbol:

Conditional compilation symbol

Do the same for Windows.

Then you can write conditional steps like the below:

    class Program
{
    static void Main(string[] args)
    {
#if MACOS
        Console.WriteLine("OSX");
#elif WINDOWS
        Console.WriteLine("Windows");
#endif

        Console.Read();
    }

Depending on your chosen build configuration .. you'll either get:

OSX:

OSX

Windows:

Windows

like image 21
Simon Whitehead Avatar answered Oct 17 '22 06:10

Simon Whitehead