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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM