25

After installing VS 2015, running csc.exe from command line causes this message to be displayed to console:

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

The link redirects to Roslyn's repository at GitHub.
So, is the a way to run "compilers that support newer versions" (Roslyn) from command line?

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • 1
    Which `csc.exe` are you running? What version number does it report? When I run `csc.exe` from the command line - with the developer command prompt for VS2015 - I see version 1.0.0.50618, and that's Roslyn. – Jon Skeet Aug 07 '15 at 10:51
  • @JonSkeet: I'm running `csc` from "%Windir%\Microsoft.NET\Framework64\v4.0.30319". I'm not using batch file for developer command prompt, it's just a cmd without any special settings. – Dennis Aug 07 '15 at 10:56
  • 1
    Then I suggest you use `csc` from msbuild instead :) See my answer. If you want to run developer commands, using the developer command prompt feels like a wise choice... – Jon Skeet Aug 07 '15 at 10:57
  • 1
    It's not an exact duplicate, but check out this question - http://stackoverflow.com/questions/31698319/csc-version-at-computer/31698761. – Luaan Aug 07 '15 at 10:59

3 Answers3

32

It sounds like your path is inappropriate, basically. If you open the "Developer Command Prompt for VS2015" you should have $ProgramFiles(x86)$\MSBuild\14.0\bin early in your path - and the csc.exe in there is Roslyn.

I suspect you're running the version in c:\Windows\Microsoft.NET\Framework\4.0.30319 or similar - which is the legacy one, basically.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for the path, where new compiler is located. I've really tried to search it, but without any success. – Dennis Aug 07 '15 at 11:00
  • technically it's not %ProgramFilesx86%, should be %ProgramFiles(x86)% – nrudnyk Oct 10 '17 at 10:11
  • 1
    @nrudnyk: Fixed. – Jon Skeet Oct 10 '17 at 10:19
  • What if you don't have Visual Studio installed? Is there a way to install the Roslyn compilers stand-alone? – Jonathan Gilbert Oct 04 '18 at 18:50
  • @JonathanGilbert: Installing the .NET Core SDK would also install Roslyn "via" dotnet... I don't know of a way of getting the csc.exe experience without installing VS. (Maybe the .NET SDK? Haven't used that for a while...) – Jon Skeet Oct 04 '18 at 19:01
  • 2
    I did a bit more digging, and found that you can install just the Build Tools for Visual Studio: https://go.microsoft.com/fwlink/?linkid=840931 You still go through the Visual Studio Installer, but this doesn't install the VS Shell or anything heavy-weight. You're just left with a link to a Developer Command Prompt and a CSC on the PATH that does exactly what I want. :-) – Jonathan Gilbert Oct 04 '18 at 20:28
4

Roslyn from command line('cmd'), Windows 10 scenario example:
( Note: No need Visual Studio installed, but only the .NET core )

  1. Open 'cmd' and create folder "dotnet-csharp-tools":

    D:>mkdir "dotnet-csharp-tools"

  2. Navigate to folder "dotnet-csharp-tools":

    D:>cd "dotnet-csharp-tools"

  3. In folder "dotnet-csharp-tools" download 'nuget.exe' latest version from:

    https://www.nuget.org/downloads

  4. Check name of the last version of 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform' from:

    https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/

    For example: 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform -Version 3.6.0'

  5. From 'cmd'(opened folder "dotnet-csharp-tools"), run command:

    D:\dotnet-csharp-tools>nuget install Microsoft.CodeDom.Providers.DotNetCompilerPlatform -Version 3.6.0

  6. From 'cmd' navigate to 'D:\dotnet-csharp-tools\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0\tools\Roslyn472'(warning : folder name 'Roslyn472' may be different, if is other version)

    D:\dotnet-csharp-tools>cd Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0\tools\Roslyn472

  7. From 'File explorer' find 'csc.exe'(in the current folder 'Roslyn472'). Make copy of 'csc.exe' with name 'csc-roslyn.exe'(name can be whatever).

  8. For 'Windows 10', open:

    'Edit system environment variables' -> 'System variables' -> 'path' -> 'Edit' -> 'New' -> D:\dotnet-csharp-tools\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0\tools\Roslyn472

  9. Close and open again 'cmd'(the command prompt). This 'cmd' restart needed, because 'system environment variables' are edited.

  10. Check if 'csc-roslyn' is recognized by 'cmd' by run command:

    csc-roslyn

  11. Create folder 'D:\csharp-projects'(folder name can be whatever) and create in 'D:\csharp-projects' C# source files, for example:

Vehicle.cs

class Vehicle
{
    private string name = "unknown";
    private int producedYear = -1;

    public Vehicle(string name, int producedYear)
    {
        this.Name = name;
        this.ProducedYear = producedYear;
    }

    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

    public int ProducedYear
    {
        get { return this.producedYear; }
        set { this.producedYear = value; }
    }
}

Car.cs

class Car : Vehicle
{
    private string maker = "unknown";

    public Car(string name, int age, string maker)
    : base(name, age)
    {
        this.Maker = maker;
    }

    public string Maker
    {
        get { return this.maker; }
        set { this.maker = value; }
    }

    public override string ToString()
    {
        return $"{this.Name}, {this.ProducedYear}, {this.Maker}";
    }
}

Autoservice.cs

using System;

class Autoservice
{
    public static void Main()
    {
        
        Car car1 = new Car("Ford Taunus", 1971, "Ford");
        Car car2 = new Car("Opel Ascona", 1978, "Opel");
        Car car3 = new Car("Saab 900", 1984, "Saab");

        Console.WriteLine(car1);
        Console.WriteLine(car2);
        Console.WriteLine(car3);
    }
}
  1. Open 'D:\csharp-projects' from 'cmd'(the command prompt) and run command:

    csc-roslyn /target:exe /out:Autoservice.exe Vehicle.cs Car.cs Autoservice.cs

  2. Run from 'cmd':

    Autoservice.exe

  3. Result should be:

Ford Taunus, 1971, Ford
Opel Ascona, 1978, Opel
Saab 900, 1984, Saab

Ted
  • 843
  • 10
  • 13
  • _Standalone Roslyn compiler installation_ - Thanks for this, In case someone is trying the above command and nuget install fails ( 404, not found) , use the following command. `nuget install Microsoft.CodeDom.Providers.DotNetCompilerPlatform -Version 3.6.0 -Source https://api.nuget.org/v3/index.json` **-Source** option enables the use of V3 nuget endpoint instead of V2. – Rishabh Puri Dec 22 '21 at 08:32
0

I suspect the location of the Roslyn compiler moves around a lot based on the Visual Studio you're running.

I was able to find mine by performing a search like this:

cd "\Program Files (x86)"

dir /s csc*.* | findstr Roslyn

My particular csc.exe was located in:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Roslyn\csc.exe

StayCool
  • 520
  • 6
  • 17