Fishfish
Fishfish

Reputation: 163

Visual Studio conditional compilation for OS

I know that there is a way to conditionally compile for target frameworks, e.g., #if net461 ....#elif ....

But is there a way to conditionally compile for a specific OS? Like target _os_MAC or target_os_win.

If there is, where are the documentation or tutorials on 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, e.g., from net461 to net471?

Upvotes: 13

Views: 4877

Answers (2)

Simon Whitehead
Simon Whitehead

Reputation: 65049

This answer assumes you're asking about custom preprocessor 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("OS X");
        #elif WINDOWS
            Console.WriteLine("Windows");
        #endif

        Console.Read();
    }

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

OS X:

OS X

Windows:

Windows

Upvotes: 4

Erndob
Erndob

Reputation: 2612

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

Upvotes: 23

Related Questions