In old .NET we used to be able to run the csc
compiler to compile a single .cs file or several files.
With .NET Core we have dotnet build
that insists on having a proper project file. Is there a stand-alone command line compiler that would allow to compile source code files without having a project (and listing referenced dependencies on the same command line)?
On Linux, when I have the old csc
and the new .NET Core installed, I get these timings:
[root@li1742-80 test]# time dotnet build
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
test -> /root/test/bin/Debug/netcoreapp2.0/test.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:03.94
real 0m7.027s
user 0m5.714s
sys 0m0.838s
[root@li1742-80 test]# time csc Program.cs
Microsoft (R) Visual C# Compiler version 2.3.0.61801 (3722bb71)
Copyright (C) Microsoft Corporation. All rights reserved.
real 0m0.613s
user 0m0.522s
sys 0m0.071s
[root@li1742-80 test]#
Note 7 seconds with .NET Core versus several hundred milliseconds with the old csc
for the same file, Program.cs
.
I'd like to be able to compile as fast with .NET Core as I used to be able with csc
.
Yes, it is possible to compile a single file with csc or vbc compilers in .NET Core.
To invoke the Roslyn compiler directly it is necessary to use the command line driver csc.{exe|dll} and since Roslyn in contrast to the old csc.exe does not reference mscorlib.dll implicitly it is necessary to pass a reference to the required dependencies, i.e.
System.Runtime
andSystem.Private.CoreLib
libraries and any other required references. The following listing shows how to compile the following Hello, World! program.Using WSL with Ubuntu 16.04 (Xenial Xerus) and dotnet-sdk-2.0.0 installed:
The required dependencies, which are passed to the compiler, are different on different platforms, i.e. on Windows it is enough to pass
System.Runtime.dll
andSystem.Console.dll
while on Ubuntu 16.04 it is necessary to pass in additionSystem.Private.CoreLib.dll
. Different SDK versions will have Roslyn and command line drivers located in different places - the SDK layout changes between versions - and the newest 2.2.2 SDK ships withcsc.dll
andvbc.dll
instead ofcsc.exe
andvbc.exe
. Therefore, before using this method it is necessary to check your SDK layout.Detailed explanation
The Roslyn compiler was designed in a bit different way than the previously used
csc.exe
andvbc.exe
compilers. First of all, Roslyn is written in C# and VB.NET and is a managed .NET application. On Windows it used mainly as a common service running in a server processVBCSCompiler.exe
(.dll). However, Roslyn ships with managed command line drivers,csc.exe
andvbc.exe
(the latest .NET SDK versions ship withcsc.dll
andvbc.dll
) which can be used to compile source files directly from the command line. Anyway, it is exactly what the build system in .NET does, invoking Roslyn via the command line. Running a simpledotnet csc.exe -help
command will print usage information which will guide in using the compiler directly from the command line (see the last listing).The major difference between old native compilers and Roslyn is due to the fact that the latter is a managed application is a startup time. Roslyn, even after being compiled to R2R native assemblies (
Ready To Run
), would need to start by loading the whole .NET framework, initializing it and then loading Roslyn assemblies and starting the compilation process. It is always a bit slower than running the native compiler, however, as can be seen from above timings, not that much slower.There was a new documentation article added to the
corefx
repository describing Advanced scenario - Build and run application code with csc/vbc and CoreRun. Anyone interested can use it as a guideline how to work at the low level of .NET Core.