mcs(1)

๐Ÿ’– mcs โ€” Turbo C# Compiler ๐Ÿ’–

okay so you're looking at mcs(1), from the General Commands Manual section of the manual pages ๐ŸŽ€ โ€” this is the Turbo C# Compiler, and bestie, she's a whole icon. let's get into it ๐Ÿ’…


โœจ NAME โœจ

mcs โ€” the Turbo C# Compiler ๐Ÿฆ‹


โœจ SYNOPSIS โœจ

mcs [option] [source-files]

โœจ DESCRIPTION โœจ

so mcs is the Turbo C# compiler (she also goes by the Mono C# compiler, iykyk), and she's a full implementation of the ECMA-334 language specification ๐Ÿ‘‘. you pass her one or more options to drive the compiler, plus a set of source files. and lowkey you can even drop extra options or arguments into a response file โ€” you reference a response file by putting the @ symbol in front of its name ๐ŸŒธ

the mcs compiler is used to compile against the latest Mono Base Class Library version and fully implements the C# 1.0, 2.0, 3.0, 4.0, 5.0 and 6.0 specifications, with partial support for C# 7.0 ๐Ÿซถ

peep the section on packages for more info, bestie ๐Ÿ“ฆ

the Turbo C# compiler accepts the same command line options that the Microsoft C# compiler does โœจ. those options can start with a slash or a dash (/checked is the same as -checked). and additionally, some GNU-like options are supported โ€” those begin with --. โญ all MCS-specific flags that are not available in the Microsoft C# compiler are available only with the GNU-style options.

C# source files must end with a .cs extension ๐Ÿ’€. compiling C# source code requires all the files that make up a library, module or executable to be provided on the command line โ€” there is no support for partial compilation, periodt. if you want the benefits of partial compilation, you compile programs into their own assemblies, and later reference them with the -r flag ๐ŸŒˆ

the Turbo C# compiler generates images (.exe files) that contain CIL byte code which can be executed by any system that implements a Common Language Infrastructure virtual machine โ€” like the Microsoft .NET runtime engine on Windows, or the Mono runtime engine on Unix systems ๐Ÿงš. executables are not bound to a specific CPU or operating system, no cap ๐Ÿ’œ

by default the Turbo C# compiler only references three assemblies: mscorlib.dll, System.dll and System.Xml.dll ๐ŸŒธ. if you want extra libraries you must manually specify them using the -pkg: command line option or the -r: command line option ๐Ÿ™ˆ. alternatively, if you want to get all of the System libraries, you can use the -pkg:dotnet command line option. slay.


โœจ OPTIONS โœจ

this is the part where the menu is long and every single item is valid, so let's go ๐Ÿ’…

  • --about โ€” shows you info about the Turbo C# compiler ๐ŸŽ€
  • --addmodule:MODULE1[,MODULE2] โ€” includes the specified modules in the resulting assembly. (modules get created by calling the compiler with the -target:module option โœจ)
  • -checked, -checked+ โ€” sets the default compilation mode to checked. this makes all the math operations checked (the default is unchecked, fyi).
  • -checked- โ€” sets the default compilation mode to unchecked. this makes all the math operations unchecked โ€” and this is the default ๐Ÿ’œ
  • -clscheck-, -clscheck+ โ€” disables or enables the Common Language Specification (CLS) checks. it's enabled by default ๐Ÿฆ‹

the Common Language Specification (CLS) defines an interoperable subset of types, plus conventions that compilers (CLS producers) and developers must follow to expose code to other programming languages (CLS consumers). so basically it's the friendship pact between languages ๐Ÿค

  • -codepage:ID โ€” specifies the code page used to process the input files from the point it is specified on. by default, files get processed in the environment-dependent native code page. the compiler will also automatically detect Unicode files that have an embedded byte mark at the beginning ๐Ÿ‘‘

other popular encodings are 28591 (Latin1), 1252 (iso-8859-1) and 65001 (UTF-8).

MCS supports a couple of shorthands: "utf8" can be used to specify utf-8 instead of using the cryptic 65001, and "reset" restores the automatic handling of code pages. these shorthands are not available on the Microsoft compiler ๐Ÿ’…

  • -define:SYMLIST, -d:SYMLIST โ€” defines the symbol listed by the semi-colon separated list SYMLIST SYMBOL. you can test this in the source code with the pre-processor, or it can be used by methods that have been tagged with the Conditional attribute ๐ŸŒธ
  • -debug, -debug+ โ€” generate debugging information. to obtain stack traces with debugging information, you need to invoke the mono runtime with the --debug flag. the debugging information is stored in a MDB file located in the same output folder as the produced assembly ๐ŸŽ€
  • -debug- โ€” do not generate debugging information. simple as that ๐Ÿ’€
  • -delaysign+ โ€” only embed the strongname public key into the assembly. the actual signing must be done in a later stage using the SN tool. this is useful to protect the private key during development ๐Ÿ”. note that delay signing can only be done using a strongname key file (not a key container). the option is equivalent to including [assembly: AssemblyDelaySign (true)] in your source code. compiler option takes precedence over the attributes, periodt.
  • -delaysign- โ€” the default. strongname (sign) the assembly using the strong name key file (or container). the option is equivalent to including [assembly: AssemblyDelaySign (false)] in your source code. compiler option takes precedence over the attributes โœจ
  • -doc:FILE โ€” extracts the C#/XML documentation from the source code and stores it in the given FILE ๐Ÿ“„
  • -errorreport โ€” this flag is ignored by Mono's C# compiler and is present only to allow MCS to be used as a CSC replacement for msbuild/xbuild ๐Ÿ™ˆ
  • --fatal โ€” this one is used for debugging the compiler. it makes the error emission generate an exception that can be caught by a debugger ๐Ÿ›
  • -filealign โ€” this flag is ignored by Mono's C# compiler and is present only to allow MCS to be used as a CSC replacement for msbuild/xbuild (same vibe as -errorreport, bestie) ๐Ÿ’œ
  • -fullpaths โ€” any source code error or warning issued by the compiler includes the file name only by default. this option causes the compiler to issue the absolute file path instead ๐Ÿ—บ๏ธ
  • -keyfile:KEYFILE โ€” strongname (sign) the output assembly using the key pair present in the specified strong name key file (snk). a full key pair is required by default (or when using delaysign-). a file containing only the public key can be used with delaysign+. the option is equivalent to including [assembly: AssemblyKeyFile ("KEYFILE")] in your source code. compiler option takes precedence over the attributes ๐Ÿ”‘
  • -keycontainer:CONTAINER โ€” strongname (sign) the output assembly using the key pair present in the specified container. note that delaysign+ is ignored when using key containers ๐Ÿ‘€. the option is equivalent to including [assembly: AssemblyKeyName ("CONTAINER")] in your source code. compiler option takes precedence over the attributes.
  • -langversion:TEXT โ€” this option specifies the version of the language to use. the feature set is different in each C# version, and this switch can force the compiler to allow only a subset of the features ๐ŸŒˆ. the possible values are:
    • Default โ€” instructs the compiler to use the latest version. equivalent to omitting the switch (this currently defaults to the C# 6.0 language specification).
    • ISO-1 โ€” restrict the compiler to use only first ISO standardized features. using features like generics, static classes, or anonymous methods will lead to error ๐Ÿ’€
    • ISO-2 โ€” restrict the compiler to use only the second ISO standardized features. this allows the use of generics, static classes, iterators and anonymous methods, for example.
    • 3 โ€” restrict the compiler to use only the features available in C# 3.0 (a superset of ISO-1 and ISO-2).
    • 4 โ€” restrict the compiler to use only the features available in the C# 4.0 specification.
    • 5 โ€” restrict the compiler to use only the features available in the C# 5.0 specification.
    • 6 โ€” restrict the compiler to use only the features available in the C# 6.0 specification.
    • experimental โ€” enables unstable features from upcoming versions of the language ๐Ÿ”ฅ
notice that this flag only restricts the language features available to the programmer. the version of the produced assemblies can be controlled using the SDK option ๐Ÿ‘‘
  • -lib:PATHLIST โ€” each path specified in the comma-separated list directs the compiler to look for libraries in that specified path ๐Ÿ“š
  • -L PATH โ€” directs the compiler to look for libraries in the specified path. you can provide multiple paths by using the option multiple times โœจ
  • -main:CLASS โ€” tells the compiler which CLASS contains the entry point. super useful when you're compiling several classes that each have a Main method ๐Ÿšช
  • -nostdlib, -nostdlib+ โ€” use this flag if you want to compile the core library. it makes the compiler load its internal types from the assembly being compiled ๐Ÿงš
  • -noconfig, -noconfig+ โ€” disables the default compiler configuration from being loaded. the compiler by default has references to the system assemblies ๐ŸŒธ
  • -nowarn:WARNLIST โ€” makes the compiler ignore warnings specified in the comma-separated list WARNLIST ๐Ÿคซ
  • -optimize, -optimize+, -optimize- โ€” controls compiler code generation optimizations on the code. using -optimize or -optimize+ turns optimizations on, -optimize- turns them off. the default in mcs is optimize-. the option can be mixed with -debug, but for the best debugging experience it's recommended to leave the options off ๐Ÿซถ
  • -out:FNAME, -o FNAME โ€” names the output file to be generated ๐Ÿ“
  • --parse โ€” used for benchmarking. the compiler will only parse its input files โฑ๏ธ
  • -pathmap:K=V[,Kn=Vn] โ€” sets a mapping for source path names used in generated output ๐Ÿ—บ๏ธ
  • -pkg:package1[,packageN] โ€” reference assemblies for the given packages ๐Ÿ“ฆ

the compiler will invoke pkg-config --libs on the set of packages specified on the command line to obtain libraries and directories to compile the code.

this is typically used with third party components, like this:

$ mcs -pkg:gtk-sharp demo.cs

...to be continued, bestie โ€” part 2 is coming ๐Ÿฆ‹

  • -pkg:dotnet โ€” tells the compiler to reference the System.* libraries that a normal dotnet framework install has lying around. Heads up though: this does NOT include all the Mono libraries, only the System.* ones. It's a lil convenience shortcut for the porting girlies. ๐Ÿซถ
  • -platform:ARCH โ€” sets your target platform, bestie. The values you can pass are: anycpu, anycpu32bitpreferred, arm, x86, x64 or itanium. And the default? anycpu. iconic default behavior tbh.
  • -resource:RESOURCE[,ID] โ€” embeds the resource file you point it at. The optional ID lets you give that resource a different name. Skip the ID and the resource name is just the file name. simple as. โœจ
  • -linkresource:RESOURCE[,ID] โ€” links to the RESOURCE you specify. The optional ID lets you name the linked resource. linky linky.
  • -r:ASSEMBLY1[,ASSEMBLY2], -reference ASSEMBLY1[,ASSEMBLY2] โ€” references the named assemblies so you can use classes from them in your program. The assembly gets loaded from either the system directory where all the assemblies live, or from the path you explicitly gave with the -L option.

You can also split assemblies with a semicolon instead of a comma. options options options ๐Ÿ’…

  • -reference:ALIAS=ASSEMBLY โ€” extern alias reference support for C#. ๐ŸŽ€

So like, if you've got different assemblies that hand you the same types, extern alias support lets you give them names so your software can tell them apart. The types from ASSEMBLY get exposed as ALIAS, and then in your C# source you gotta do:

extern alias ALIAS;

to bring it into your namespace. Example time: to deal with two graphics libraries that both define "Graphics.Point", one in "OpenGL.dll" and one in "Postscript.dll", you'd invoke the compiler like this:

mcs -r:Postscript=Postscript.dll -r:OpenGL=OpenGL.dll

And in your source code, you'd write:

extern alias Postscript;
extern alias OpenGL;

class X {
     // This is a Graphics.Point from Postscrip.dll
     Postscript.Point p = new Postscript.Point ();

     // This is a Graphics.Point from OpenGL.dll
     OpenGL.Point p = new OpenGL.Point ();
}

tell me that's not the cleanest thing you've ever seen ๐Ÿ˜ญ

  • -recurse:PATTERN, --recurse PATTERN โ€” does recursive compilation using the pattern you give it. In Unix the shell does the globbing for you, so you might wanna use it like this:
$ mcs -recurse:'*.cs'

๐Ÿฆ‹

  • -sdk:VERSION โ€” specifies which version of Base Class Library assemblies to use for compilation. These predefined values are valid: 2, 4 (default), plus any custom value. The predefined version number means... when a custom value is specified, mcs will try to find the Base Class Libraries in the mono installed location PREFIX/lib/mono/<value>. paths, my beloved.
  • --shell โ€” starts the compiler up in interactive mode, giving you a C# shell for statements and expressions. Shortcut? Just use the csharp command directly. ๐Ÿ”ฅ
  • --stacktrace โ€” generates a stack trace at the time the error is reported. Super useful for debugging the compiler itself. dev girlies rise.
  • -target:KIND, -t:KIND โ€” specifies your desired target. Possible values: exe (plain executable), winexe (Windows.Forms executable), library (component libraries) and module (partial library). pick your fighter ๐Ÿ’…
  • --timestamp โ€” another debugging flag. Displays the times at various points in the compilation process. โฐ
  • -unsafe, -unsafe+ โ€” enables compilation of unsafe code. living dangerously, i see. ๐Ÿ˜Œ
  • -v โ€” debugging! Turns on verbose yacc parsing.
  • --version โ€” shows the compiler version. โญ
  • -warnaserror, -warnaserror+ โ€” every single compiler warning gets reported as an error. no warnings allowed, periodt.
  • -warnaserror:W1,[Wn], -warnaserror+:W1,[Wn] โ€” treats one or more compiler warnings as errors. just the ones you pick.
  • -warnaserror-:W1,[Wn] โ€” sets one or more compiler warnings to be always treated as warnings. This becomes useful when you use it together with -warnaserror. the exception to the rule, love that for you.
  • -warn:LEVEL โ€” sets the warning level. 0 is the lowest, 4 is the highest. Default is 4. ๐Ÿ“ˆ
  • -win32res:FILE โ€” specifies a Win32 resource file (.res) to be bundled into the resulting assembly. ๐ŸŽ
  • -win32icon:FILE โ€” attaches the icon in FILE on the output into the resulting assembly. cute lil icon moment.
  • -- โ€” use this to stop option parsing, so option-looking parameters can be passed on the command line. the escape hatch, bestie.

๐Ÿ“ฆ PACKAGES AND LIBRARIES ๐Ÿ“ฆ

When you're referencing an assembly, if the assembly name is a path, the compiler tries to load the assembly at that path. If it doesn't, then the compiler tries loading the assembly from the current directory, the compiler base directory, and if it's not found in any of those places, in the directories specified as arguments to the -lib: command argument.

The compiler uses the library path to locate libraries, and it can reference libraries from a particular package if that directory is used. To make packages easier to use, the C# compiler includes the -pkg: command line option, which loads specific collections of libraries.

Libraries visible to the compiler are stored relative to the installation prefix under PREFIX/lib/mono/, which is called the PACKAGEBASE. And the defaults for mcs, gmcs and smcs are:

  • mcs โ€” references the PACKAGEBASE/1.0 directory
  • gmcs โ€” references the PACKAGEBASE/2.0 directory
  • smcs โ€” references the PACKAGEBASE/2.1 directory

Those are the only runtime profiles that exist. Even though other directories exist (like 3.0 and 3.5), those aren't really runtime profiles โ€” they're just placeholders for extra libraries that build on the 2.0 foundation. the more you know ๐Ÿ’ซ

Software providers distribute software that's installed relative to the PACKAGEBASE directory. This is integrated into the gacutil tool, which not only installs public assemblies into the Global Assembly Cache (GAC) but also installs them into the PACKAGEBASE/PKG directory (where PKG is the name passed to the -package flag of gacutil).

As a developer, if you wanna consume the Gtk# libraries, you'd invoke the compiler like this:

$ mcs -pkg:gtk-sharp-2.0 main.cs

The -pkg: option tells the compiler to fetch the definitions for gtk-sharp-2.0 from pkg-config, which is equivalent to passing the C# compiler the output of:

$ pkg-config --libs gtk-sharp-2.0

Usually this just references the libraries from PACKAGEBASE/PKG. ๐Ÿซถ

Even though there are directory names for 3.0 and 3.5, that does not mean there are 3.0 and 3.5 compiler editions or profiles. Those are merely new libraries that have to be manually referenced, either with the proper -pkg: invocation, or by referencing the libraries directly.


๐Ÿ”ฎ SPECIAL DEFINES ๐Ÿ”ฎ

The TRACE and DEBUG defines have a special meaning to the compiler. iykyk.

By default, calls to methods and properties in the System.Diagnostics.Trace class are not generated unless the TRACE symbol is defined (either through a "#define TRACE" in your source code, or by using --define TRACE on the command line).

By default, calls to methods and properties in the System.Diagnostics.Debug class are not generated unless the DEBUG symbol is defined (either through a "#define DEBUG" in your source code, or by using --define DEBUG on the command line).

Bestie, real talk: the effect of defining TRACE and DEBUG is a global setting, even if they're only defined in a single file. ๐ŸŒ

๐Ÿ› DEBUGGING SUPPORT ๐Ÿ›

When you use the "-debug" flag, MCS generates a file with the .mdb extension that contains the debugging information for the generated assembly. That file gets consumed by the Mono debugger (mdb). debugging era fr.


๐ŸŒˆ ENVIRONMENT VARIABLES ๐ŸŒˆ

  • MCS_COLORS โ€” if this variable is set, it contains a string in the form "foreground,background" that says which color to use to display errors on some terminals. ๐ŸŽจ

The background is optional and defaults to your terminal's current background.

The possible colors for foreground are: black, red, brightred, green, brightgreen, yellow, brightyellow, blue, brightblue, magenta, brightmagenta, cyan, brightcyan, grey, white and brightwhite.

The possible colors for background are: black, red, green, yellow, blue, magenta, cyan, grey and white.

For example, you could set these variables from your shell:

export MCS_COLORS
MCS_COLORS=errors=brightwhite,red

And you can disable the built-in color scheme by setting this variable to "disable". no more pretty colors ๐Ÿ˜ญ


๐Ÿ“ NOTES ๐Ÿ“

During compilation the MCS compiler defines the __MonoCS__ symbol. You can use it in pre-processor instructions to compile Mono C# compiler specific code.

Ok but listen: this symbol is only to test for the compiler, and it is not useful for distinguishing compilation or deployment platforms. don't get it twisted bestie ๐Ÿ™ˆ

๐Ÿ‘‘ AUTHORS ๐Ÿ‘‘

The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap, Martin Baulig, Marek Safar and Raja Harinath. The development was funded by Ximian, Novell and Marek Safar. legends, all of them ๐Ÿ’–


๐Ÿ“œ LICENSE ๐Ÿ“œ

The Mono Compiler Suite is released under the terms of the GNU GPL or the MIT X11. Please read the accompanying COPYING file for details. Alternative licensing for the compiler is available from Xamarin. read the fine print, bestie ๐ŸŽ€


๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

csharp(1), mono(1), pkg-config(1), sn(1)


๐Ÿ’€ BUGS ๐Ÿ’€

To report bugs in the compiler, you must file them on our bug tracking system, at: http://www.mono-project.com/community/bugs/

do it. the devs are begging you. ๐Ÿ’…


๐Ÿ“ฌ MAILING LIST ๐Ÿ“ฌ

The Mono Mailing lists are listed at http://www.mono-project.com/community/help/mailing-lists/


โญ MORE INFORMATION โญ

The Mono C# compiler was developed by Novell, Inc (http://www.novell.com) and Xamarin Inc (http://www.xamarin.com) is based on the ECMA C# language standard available here: http://www.ecma.ch/ecma1/STAND/ecma-334.htm

The home page for the Mono C# compiler is at http://www.mono-project.com/docs/about-mono/languages/csharp/


and that's the whole page bestie ๐Ÿซถ this lil manual is dated 6 January 2001 and it's mcs(1). go forth and compile slayfully ๐Ÿ‘‘โœจ