LinuxCommandLibrary

csc

Compile C# programs

TLDR

Compile one or more C# files to a CIL executable

$ csc [path/to/input_file1.cs path/to/input_file2.cs ...]
copy

Specify the output filename
$ csc /out:[path/to/file] [path/to/input_file.cs]
copy

Compile into a .dll library instead of an executable
$ csc /target:library [path/to/input_file.cs]
copy

Reference another assembly
$ csc /reference:[path/to/library.dll] [path/to/input_file.cs]
copy

Embed a resource
$ csc /resource:[path/to/resource_file] [path/to/input_file.cs]
copy

Automatically generate XML documentation
$ csc /doc:[path/to/output.xml] [path/to/input_file.cs]
copy

Specify an icon
$ csc /win32icon:[path/to/icon.ico] [path/to/input_file.cs]
copy

Strongly-name the resulting assembly with a keyfile
$ csc /keyfile:[path/to/keyfile] [path/to/input_file.cs]
copy

SYNOPSIS

csc [-option...] source-files

PARAMETERS

-out:
    Name of output file or assembly

-target:
    Target type: exe, winexe, library, module, winmdobj

-reference: or -r:
    Reference metadata from assemblies

-debug[+|-]
    Emit debugging information

-optimize[+|-] or -o[+|-]
    Enable optimizations

-define:
    Define conditional compilation symbols

-unsafe[+|-]
    Allow unsafe code blocks

-platform:
    Target platform: anycpu, anycpu32bitpreferred, x86, x64, arm, arm64

-warn:
    Set warning level (0-4)

-recurse:
    Recursively include source files matching pattern

DESCRIPTION

csc is the official command-line compiler for the C# programming language in the .NET ecosystem. It translates C# source files (.cs) into executable assemblies (.exe or .dll), modules, or other targets. On Linux, it is available via the Mono framework or the .NET SDK (formerly .NET Core).

Primarily used for compiling single files or small projects, csc supports advanced features like optimization, debugging symbols, assembly references, conditional compilation, and unsafe code. It emits Microsoft Intermediate Language (MSIL) code, which is JIT-compiled at runtime by the .NET runtime.

Basic usage: csc hello.cs produces hello.exe. For libraries: csc -target:library -out:mylib.dll *.cs. It integrates with build tools like MSBuild but is ideal for quick compilations or scripts.

Options mimic Windows behavior for cross-platform consistency. Always consult man csc or Microsoft docs for full details, as options evolve with .NET versions. Requires dotnet-sdk or Mono installed.

CAVEATS

Not installed by default; install via dotnet-sdk or Mono. Prefer dotnet build for complex projects. Hundreds of options exist—use man csc or csc -help. Version-specific behaviors apply.

RESPONSE FILES

Use @filename to read options from a text file, useful for long argument lists.
Example: csc @build.rsp hello.cs

EMBEDDED RESOURCES

-resource:[[,identifier][,private][,access]] embeds files as resources in assembly.

HISTORY

Developed by Microsoft in 2001 for .NET Framework. Ported to Linux by Mono project (2004) for cross-platform use. Official Linux support via .NET Core 1.0 (2016), now .NET 8+ with improved performance and AOT compilation.

SEE ALSO

mcs(1), dotnet(1), msbuild(1), csc(1)

Copied to clipboard