VS C#命令行编译工具CSC使用入门

VS C#命令行编译工具CSC使用入门

撰写日期:2016/04/21
更新日期:2016/04/21
博客地址:http://www.cnblogs.com/gibbonnet/p/5420548.html

CSC.exe: Visual C# Compiler

任务场景

  • 调试
  • 多文件编译
  • 生成DLL/引用DLL

资源

官方文档:https://msdn.microsoft.com/en-us/library/2fdbz5xd.aspx

任务

为CSC设置环境变量

运行VSVARS32.BAT
https://msdn.microsoft.com/en-us/library/1700bbwd.aspx

部署程序

安装.NET: https://msdn.microsoft.com/en-us/library/ee942965.aspx

Deploying Applications, Services, and Components: https://msdn.microsoft.com/en-us/library/wtzawcsz.aspx

基本操作

Compiles File.cs producing File.exe:

csc File.cs 

Compiles File.cs producing File.dll:

csc /target:library File.cs

Compiles File.cs and creates My.exe:

csc /out:My.exe File.cs

Compiles all the C# files in the current directory, with optimizations on and defines the DEBUG symbol. The output is File2.exe:

csc /define:DEBUG /optimize /out:File2.exe *.cs

Compiles all the C# files in the current directory producing a debug version of File2.dll. No logo and no warnings are displayed:

csc /target:library /out:File2.dll /warn:0 /nologo /debug *.cs

Compiles all the C# files in the current directory to Something.xyz (a DLL):

csc /target:library /out:Something.xyz *.cs

调试

多文件编译

csc /define:DEBUG /optimize /out:File2.exe *.cs

模块化

知识:运行时如何定位汇编程序

生成和引用DLL

csc /target:library /out:File2.dll /warn:0 /nologo /debug *.cs
csc /target:library /out:lib/Class1.dll Class1.cs
csc /lib:lib /reference:Class1.dll HelloWorld.cs

但是HelloWorld.exe运行时,需要和Class1.dll在同一个目录

The /reference option causes the compiler to import public type information in the specified file into the current project, thus enabling you to reference metadata from the specified assembly files.

/reference:[alias=]filename
/reference:filename

The /lib option specifies the location of assemblies referenced by means of the /reference (C# Compiler Options) option.

/lib:dir1[,dir2]

问题:如何引用多个DLL?

什么是Module:https://social.msdn.microsoft.com/Forums/zh-CN/79dbe468-b0b5-47da-9012-40f83a989465/what-does-it-mean-by-a-module-reference-in-c?forum=netfxbcl

Module VS Assembly:Module是编译单元,Assemly是部署单元。
https://social.msdn.microsoft.com/Forums/en-US/c83f174d-03f4-4b4c-930c-2d2a4dc54356/using-modules-netmodule?forum=Vsexpressvcs

命令行选项

  • 输出文件:输出文件名、生成目标类型、生成文档地址、平台
  • 输入文件:包含文件、引用元数据、链接模块、嵌入元数据、分析器
  • 资源:win32资源、win32图标、win32声明文件、嵌入特特定资源
  • 代码生成:??
  • 错误和警告:
  • 语言
  • 安全
  • 其他
  • 高级

输出选项

输出选项:/target:appcontainerexe, /target:exe, /target:library, /target:module, /target:winexe, or /target:winmdobj

/target:appcontainerexe
	To create an .exe file for Windows 8.x Store apps.
/target:exe
    To create an .exe file.
/target:library
    To create a code library.
/target:module
    To create a module.
/target:winexe
    To create a Windows program.
/target:winmdobj
    To create an intermediate .winmdobj file.

参考:按类别排序的命令行选项 https://msdn.microsoft.com/en-us/library/6s2x2bzy.aspx

外接

linker

al.exe:Microsoft(R) Assembly Linker

al是不是具有将多个module合成一个dll的功能?

SCSC 2.0

使用 C# 2.0 命令行编译器

代码注释与生成文档

官方参考:https://msdn.microsoft.com/en-us/library/5ast78ax.aspx

范例:

using System;
namespace gibbon.learning.csc
{
    ///
    /// Summary description for App.
    ///
    class App
    {
        /// Main Method
        static void Main(string[] args)
        {
            // Class Class1.SayHello
            Class1.SayHello("Hello VS C# .NET");
        }
    }

    /// <summary>
    ///     Class1具有sayHello静态方法
    /// </summary>
    class Class1
    {
        /// <summary>
        ///     sayHello方法
        ///     <param name="message">消息</param>
        ///     <returns>结果</returns>
        /// </summary>
        public static int SayHello(string message)
        {
            Console.WriteLine(message);
            return 0;
        }
    }
}

生成的XML

<?xml version="1.0"?>
<doc>
    <assembly>
        <name>DemoCommont</name>
    </assembly>
    <members>
        <member name="T:gibbon.learning.csc.App">  
          
             Summary description for App.    
        
        </member>
        <member name="M:gibbon.learning.csc.App.Main(System.String[])">
            Main Method
        </member>
        <member name="T:gibbon.learning.csc.Class1">
            <summary>
                Class1具有sayHello静态方法
            </summary>
        </member>
        <member name="M:gibbon.learning.csc.Class1.SayHello(System.String)">
            <summary>
                sayHello方法
                <param name="message">消息</param>
                <returns>结果</returns>
            </summary>
        </member>
    </members>
</doc>

posted @ 2016-04-22 11:19  Gibbon  阅读(3349)  评论(0编辑  收藏  举报