CSharp Tools - Compiler

本文介绍了如何在命令行中使用csc.exe编译C#代码,讲解了C#编译器将源代码编译为MSIL并由.NET运行时管理的过程。讨论了JIT编译、编译错误CS1619的解决方法,以及如何编译为exe、dll文件,包括设置调试模式、生成DLL和引用不同源文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*

Author: Jiangong SUN

*/

CSharp compiler is invoked at the command line using csc.exe file located <C:\Windows\Microsoft.NET\Framework\v2.0.50727>.

CSharp Compiler compiles CSharp code to MSIL(Intermediate Language) at compilation time in CLR (Common Language Runtime).  The compiled code(exe or dll) is managed code, and is managed by Garbage Collector(GC).

When you execute the exe or dll file, it will be compiled by JIT(Just-In-Time) compiler, and generate native machine code for specific CPU.


When you want to compile a class using csc.exe you may encounter this error:CS 1619: Cannot create temporary file


All you need to do is run cmd.exe as "Administrator". Re-run the compiler and it will compile the csharp file to an executable(.exe). 


Compile csharp code to DLL (dynamic link library). 


Note: .exe and .dll are all assembly files.

Compile CSharp code to named executable file.


Compile all the .CS files in current directory with a name of executable, the code mode can be DEBUG or RELEASE with optimization.

It will generate code in mode "DEBUG" like:

#if DEBUG
            LazySingleton s2 = LazySingleton.Instance;
            int num2 = s2.MultiplyBy5(20);
            Console.WriteLine(num2);
#endif


Compile all the .CS files in current directory to a named DLL file "/target:library /out:LazyDLL.dll", and generate debug file(.pdb) with option "/debug", and doesn't display Microsoft copyright with option "/nologo", with warning level (0-4) "/warn:0".


Compile all the .CS file in current directory to a user personalized type of DLL.


Compile a class inside a .CS file who reference another class in another .CS file.

For example: 


To compile <ConsoleApplication.cs>, you need to reference <ConsoleClass.cs> before it.



I hope this post can do help in your work. Enjoy coding!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值