C#环境搭建,以及C#编译器的使用

搭建nuget包管理器

HomePage

windows环境下,可以下载安装包:Download

使用最新版本的C#编译器

C# 5.0之后,微软将csc开源并独立运行,其项目命名为——roslyn

Get the C# compiler before v5.0

C# 5.0 之前的版本,编译器csc集成在 .Net Framework 中,一般在以下目录中可以找到:

C:\Windows\Microsoft.NET\Framework64\v[版本号]\csc.exe

Run the compiler of .Net Core 2.0+

一般可以通过 dotnet 命令直接调用C#编译器,因为编译器已经作为dll包含在了 .Net Core 的安装包中,路径在:

  • windows
    C:\Program Files\dotnet\sdk\v[版本号]\Roslyn\bincore\csc.dll
  • linux
    /usr/share/dotnet/sdk/v[版本号]/Roslyn/bincore/csc.dll

Get the latest csc.exe on Windows-OS

nuget install Microsoft.Net.Compilers # Install C# and VB compilers
nuget install Microsoft.CodeAnalysis # Install Language APIs and Services

C# compiler的使用

调用 C# 编译器时,不会创建任何对象 (.obj) 文件,而是直接创建输出文件。 因此,C# 编译器不需要链接器。

常用命令示例

csc File.cs
# 编译生成库文件,以 File.dll 作为输出:
csc -target:library File.cs
# 编译 File.cs 并创建 My.exe 作为输出:
csc -out:My.exe File.cs
# 编译当前目录中的所有 C# 文件,对其进行优化并定义 DEBUG 符号:
csc -define:DEBUG -optimize -out:File2.exe *.cs
# 编译生成 File2.dll 的调试版本。不显示徽标和警告:
csc -target:library -out:File2.dll -warn:0 -nologo -debug *.cs
# 将当前目录中的所有 C# 文件编译为 Something.xyz (DLL):
csc -target:library -out:Something.xyz *.cs

C# 编译器选项

选项目标
-doc指定要将已处理的文档注释写入到的 XML 文件。
-out指定输出文件。
/pdb指定 .pdb 文件的文件名和位置。
-platform指定输出平台。
-target

使用下列五个选项之一指定输出文件的格式:

-target:appcontainerexe-target:exe-target:library
-target:module-target:winexe 或 -target:winmdobj

-modulename:<string>指定源模块的名称
/lib指定通过 -reference 的方式引用的程序集的位置。
-debug指示编译器发出调试信息。
-define定义预处理器符号。
-langversion指定语言版本:默认、ISO-1、ISO-2、3、4、5、6、7、7.1、7.2、7.3 或最新版

测试程序:

// preprocessor_define.cs
// compile with: -define:DEBUG
// or uncomment the next line
// #define DEBUG
using System;
public class Test
{
    public static void Main()
    {
        #if (DEBUG)
            Console.WriteLine("xx defined");
        #else
            Console.WriteLine("xx not defined");
        #endif
    }
}

命令行编译:

csc  -define:DEBUG;TUESDAY  test.cs

C# 编译器错误

请直接查询官网:链接

转载于:https://www.cnblogs.com/brt3/p/9734519.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值