随笔 - 547  文章 - 213 评论 - 417 阅读 - 107万

msdn关于csc的命令有些不好用,现在测试一下,以免以后使用的时候出问题


msdn的描述:

•    编译 File.cs 以产生 File.exe: 
csc File.cs 
•    编译 File.cs 以产生 File.dll: 
csc 
/target:library File.cs
•    编译 File.cs 并创建 My.exe: 
csc 
/out:My.exe File.cs
•    通过使用优化和定义 DEBUG 符号,编译当前目录中所有的 C# 文件。输出为 File2.exe: 
csc 
/define:DEBUG /optimize /out:File2.exe *.cs
•    编译当前目录中所有的 C# 文件,以产生 File2.dll 的调试版本。不显示任何徽标和警告: 
csc 
/target:library /out:File2.dll /warn:0 /nologo /debug *.cs
•    将当前目录中所有的 C# 文件编译为 Something.xyz(一个 DLL): 
csc 
/target:library /out:Something.xyz *.cs

用下面的代码测试:

using System;
using System.Diagnostics;

class Class1
{
    [STAThread]
    
static void Main(string[] args)
    {                
        TextWriterTraceListener myWriter 
= 
            
new TextWriterTraceListener(System.Console.Out);
        Debug.Listeners.Add(myWriter);
        Console.WriteLine(
"Console.WriteLine is always displayed");
        Method1();
        Method2();
    }
    
    [Conditional(
"CONDITION1")]
    
public static void Method1()
    {
        Debug.Write(
"Method1 - DEBUG and CONDITION1 are specified\n");
        Trace.Write(
"Method1 - TRACE and CONDITION1 are specified\n");
    }
    
    [Conditional(
"CONDITION1"), Conditional("CONDITION2")]    
    
public static void Method2()
    {
        Debug.Write(
"Method2 - DEBUG, CONDITION1 or CONDITION2 are specified\n");
    }
}


1. 
 命令行如下;
csc CAS.cs

结果:
 生成CAS.exe
 运行CAS.exe,运行结果如下:
Console.WriteLine is always displayed

也就是说,因为没有指定编译条件,所以Method1和Method2都没有执行



2. 命令行:
csc /target:library CAS.cs
结果:
 生成CAS.dll

3. 命令行:
csc /out:MyCAS.exe CAS.cs
结果:
   生成MyCAS.exe
   运行MyCAS.exe,运行结果如下:
Console.WriteLine is always displayed

也就是说,因为没有指定编译条件,所以Method1和Method2都没有执行



4. 现在要尝试将两个文件编译在一起。
两个文件如下:
 CAS.cs
using System;
using System.Diagnostics;

class Class1
{
    [STAThread]
    
static void Main(string[] args)
    {                
        TextWriterTraceListener myWriter 
= 
            
new TextWriterTraceListener(System.Console.Out);
        Debug.Listeners.Add(myWriter);
        Console.WriteLine(
"Console.WriteLine is always displayed");
        Method1();
        Method2();
        Class2.Method3();
    }
    
    [Conditional(
"CONDITION1")]
    
public static void Method1()
    {
        Debug.Write(
"Method1 - DEBUG and CONDITION1 are specified\n");
        Trace.Write(
"Method1 - TRACE and CONDITION1 are specified\n");
    }
    
    [Conditional(
"CONDITION1"), Conditional("CONDITION2")]    
    
public static void Method2()
    {
        Debug.Write(
"Method2 - DEBUG, CONDITION1 or CONDITION2 are specified\n");
    }
}
CAS2.cs
using System;
using System.Diagnostics;

class Class2
{
    
    
public static void Method3()
    {
        Debug.Write(
"Method3 - The method is written in another file in the same folder\n");
    }
}

可以看到,CAS1.cs中的方法调用了CAS2.cs中的方法,用下面的命令行编译:

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

结果:
 生成了File2.exe
 运行File2.exe,运行结果如下:
Console.WriteLine is always displayed
Method3 
- The method is written in another file in the same folder

可以看到,在同一个文件File2.exe中,即包含了了CAS.cs中的方法,也包含了第二个文件中的方法


5.   在前面的步骤中,输入命令行回车以后,在Dos窗口中出现徽标和警告信息,例如:

Microsoft (R) Visual C# .NET 编译器版本 7.10.3052.4
用于 Microsoft (R) .NET Framework 版本 
1.1.4322
版权所有 (C) Microsoft Corporation 
2001-2002。保留所有权利。

但是使用下面的命令行:

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

就不会出现上面的徽标信息,而且成功地生成了File2.dll文件。

下面继续研究什么是徽标,什么是警告信息。
 (1) 用下面的命令行(去掉nologo):

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

结果键入回车以后Dos窗口显示如下信息:

Microsoft (R) Visual C# .NET 编译器版本 7.10.3052.4
用于 Microsoft (R) .NET Framework 版本 
1.1.4322
版权所有 (C) Microsoft Corporation 
2001-2002。保留所有权利。

说明这些就是徽标(Logo)
(2) 将CAS2.cs删除(故意制造错误),并且用下面的命令行(不显示警告信息):

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

键入回车后,Dos窗口中显示如下信息:

CAS.cs(15,3): error CS0246: 找不到类型或命名空间名称“Class2”(是否缺少 using
        指令或程序集引用?)

看来不显示错误信息不等于不显示警告信息。
(3) 恢复CAS2.cs,然后修改CAS.cs,制造一些警告信息:

using System;
using System.Diagnostics;

class Class1
{
    [STAThread]
    
static void Main(string[] args)
    {      
        
string str = "";  
        TextWriterTraceListener myWriter 
= 
            
new TextWriterTraceListener(System.Console.Out);
        Debug.Listeners.Add(myWriter);
        Console.WriteLine(
"Console.WriteLine is always displayed");
        Method1();
        Method2();
        Class2.Method3();
    }
    
    [Conditional(
"CONDITION1")]
    
public static void Method1()
    {
        Debug.Write(
"Method1 - DEBUG and CONDITION1 are specified\n");
        Trace.Write(
"Method1 - TRACE and CONDITION1 are specified\n");
    }
    
    [Conditional(
"CONDITION1"), Conditional("CONDITION2")]    
    
public static void Method2()
    {
        Debug.Write(
"Method2 - DEBUG, CONDITION1 or CONDITION2 are specified\n");
    }
}

然后用下面的命令行(显示警告信息)

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

键入回车后,Dos窗口显示如下信息:

CAS.cs(9,16): warning CS0219: 变量“str”已赋值,但其值从未使用过

这就是要警告信息。

这时,如果用下面的命令行(不显示警告信息):

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

键入回车以后Dos窗口上什么都没有
说明已经成功地隐藏了警告信息。


下面看一下csc命令行的条件编译参数与vs.net 2003编译器的对应情况:

(1)    右击项目名称,选择“属性”

2)弹出项目属性对话框

(3) 以下是对应关系:

条件编译常数: DEBUG;TRACE 对应   /d:TRACE /d:DEBUG  或者/define:TRACE /define:DEBUG

优化代码:True   对应  /optimize 

检查算法上溢/下溢 : False 对应

允许不安全代码块: False   对应

输出路径: bin\Debug  对应

Xml文档文件:对应

生成调试信息:对应

Com Interop注册:  对应



以下是msdn列出的所有csc的参数:

 

下列编译器选项按类别排序。有关按字母顺序排序的列表,请参见按字母顺序列出的 C# 编译器选项

优化

选项

用途

/filealign

指定输出文件中节的大小。

/optimize

启用/禁用优化。

输出文件

选项

用途

/doc

处理 XML 文件的文档注释。

/out

指定输出文件。

/target

使用下面的四个选项之一指定输出文件的格式:
/target:exe
/target:library
/target:module
/target:winexe

.NET Framework 程序集

选项

用途

/addmodule

指定一个或多个模块作为此程序集的一部分。

/lib

指定通过 /reference 引用的程序集的位置。

/nostdlib

不导入标准库 (mscorlib.dll)

/reference

从包含程序集的文件中导入元数据。

调试/错误检查

选项

用途

/bugreport

创建一个包含信息(该信息便于报告错误)的文件。

/checked

指定溢出数据类型边界的整数算法是否将在运行时导致异常。

/debug

发出调试信息。

/fullpaths

指定编译器输出中的文件的绝对路径。

/nowarn

禁用编译器生成指定警告的能力。

/warn

设置警告等级。

/warnaserror

将警告提升为错误。

预处理器

选项

用途

/define

定义预处理器符号。

资源

选项

用途

/linkresource

创建指向托管资源的链接。

/resource

.NET Framework 资源嵌入到输出文件中。

/win32icon

.ico 文件插入到输出文件中。

/win32res

Win32 资源插入到输出文件中。

杂项

选项

用途

@

指定响应文件。

/?

列出编译器选项到 stdout

/baseaddress

指定加载 DLL 的首选基址。

/codepage

指定编译中的所有源代码文件所使用的代码页。

/help

列出编译器选项到 stdout

/incremental

启用源代码文件的增量编译。

/main

指定 Main 方法的位置。

/noconfig

不要使用 csc.rsp 进行编译。

/nologo

取消显示编译器版权标志信息。

/recurse

搜索子目录中要编译的源文件。

/unsafe

编译使用 unsafe 关键字的代码。

/utf8output

使用 UTF-8 编码显示编译器输出。

 


可以看出,csc命令的参数与vs.net2003中的参数并不是严格对应的。该结论是否正确,还有待于进一步验证
 

 

posted on   今夜太冷  阅读(969)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示