• 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:WindowsMicrosoft.NETFramework64v[版本号]csc.exe

    Run the compiler of .Net Core 2.0+

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

    • windows
      C:Program Filesdotnetsdkv[版本号]Roslynincorecsc.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# 编译器错误

    请直接查询官网:链接

  • 相关阅读:
    Debian 添加用户
    如何让安卓手机访问内网服务器?
    数据库权限
    CentOs
    批量导入sql文件。
    使用Navicat Premium连接mysql数据库
    git 合包
    linux 下文件打包
    git 分支管理
    gcc8.2安装
  • 原文地址:https://www.cnblogs.com/brt3/p/9734519.html
Copyright © 2020-2023  润新知