C# compiler: /nostdlib option
Asked Answered
H

4

7

How is this possible not to include stdlib (mscorlib.dll) to my C# application when compiling it? As far as I know, all classes inherit System.Object class, which is defined in mscorlib.dll. What is more - types such as int are just aliases e.g. for System.Int32, which are also defined in mscorlib. Is this option ever used?

Hartzel answered 1/8, 2010 at 18:1 Comment(0)
L
10

Yes, it is used by anybody that compiles a program that doesn't run with the desktop version of the CLR. Like Silverlight, it targets .NETCore, or the Micro Framework. They have their own mscorlib.dll, of course with System.Object defined.

Here's the compiler command line of a sample Silverlight project:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig /nowarn:1701,1702 
/nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE;SILVERLIGHT
/reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\mscorlib.dll" 
  etc...
Lengel answered 1/8, 2010 at 18:8 Comment(0)
O
3

According to the docs

http://msdn.microsoft.com/en-us/library/fa13yay7(VS.80).aspx

You use it if you are trying to replace the System classes.

Oodles answered 1/8, 2010 at 18:5 Comment(2)
It is still not clear to me. Is using my own version of System.Object acceptable? I don't think CLR would allow that. Both System.Object and System.ValueType are special classes that also indicate whether my variable is of reference type or value type.Hartzel
@MarcAndreson: As I understand it, the /nostdlib compiler switch won't necessarily result in an assembly that the CLR (.NET Framework) can execute. But it produces a valid assembly that might well be executable on a different CLI-compatible platform (such as Mono, Silverlight, or your own). Given this feature, you can have one C# compiler for any of these platforms. If every platorm's C# compiler always, unconditionally tied your code specifically to its own mscorlib.dll, you would need one compiler per platform. So /nostdlib (in theory) makes the compiler reusable across platforms.Snowball
F
1

You may also want to use it if you want to build to deploy against an older framework version. Visual Studio (15, anyway) uses this option when building a project that you configured to target an older framework version. Instead of using the standard mscore, it uses one from Reference Assemblies/Microsoft/Framework/vx.y

Fiducial answered 19/1, 2016 at 16:32 Comment(0)
O
0

From MSDN - "Use this option if you want to define or create your own System namespace and objects.". Fair enough - I won't be doing that any time soon, tho'. :)

Otes answered 1/8, 2010 at 18:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.