-lib(C# 编译器选项)-lib (C# Compiler Options)

-lib 选项指定通过 -reference(C# 编译器选项)选项引用的程序集的位置 。The -lib option specifies the location of assemblies referenced by means of the -reference (C# Compiler Options) option.




在当前工作目录(调用编译器的目录)或公共语言运行时的系统目录中未找到引用的程序集时,编译器将在其中进行查找的目录。A directory for the compiler to look in if a referenced assembly is not found in the current working directory (the directory from which you are invoking the compiler) or in the common language runtime's system directory.

要在其中搜索程序集引用的一个或多个附加目录。One or more additional directories to search in for assembly references. 用逗号分隔每个附加目录的名称,中间不要有空格。Separate additional directory names with a comma, and without white space between them.


编译器按以下顺序搜索未完全限定的程序集引用:The compiler searches for assembly references that are not fully qualified in the following order:

  1. 当前工作目录。Current working directory. 该目录为从其调用编译器的目录。This is the directory from which the compiler is invoked.

  2. 公共语言运行时系统目录。The common language runtime system directory.

  3. 由 -lib 指定的目录 。Directories specified by -lib.

  4. 由 LIB 环境变量指定的目录。Directories specified by the LIB environment variable.

使用 -reference 指定程序集引用 。Use -reference to specify an assembly reference.

-lib 是累加的;每一次指定的值都追加到以前的值中 。-lib is additive; specifying it more than once appends to any prior values.

另一种使用 -lib 的方法是,将任何所需的程序集复制到工作目录中;这样可以仅将程序集名称传递给 -reference 。An alternative to using -lib is to copy into the working directory any required assemblies; this will allow you to simply pass the assembly name to -reference. 然后可以从工作目录中删除这些程序集。You can then delete the assemblies from the working directory. 由于程序集清单中未指定依赖程序集的路径,因此应用程序可以在目标计算机上启动,然后查找并使用全局程序集缓存中的程序集。Since the path to the dependent assembly is not specified in the assembly manifest, the application can be started on the target computer and will find and use the assembly in the global assembly cache.

编译器可以引用程序集并不表示公共语言运行时可以在运行时找到并加载程序集。Because the compiler can reference the assembly does not imply the common language runtime will be able to find and load the assembly at runtime. 有关运行时如何搜索引用的程序集的详细信息,请参阅运行时如何定位程序集See How the Runtime Locates Assemblies for details on how the runtime searches for referenced assemblies.

在 Visual Studio 开发环境中设置此编译器选项To set this compiler option in the Visual Studio development environment

  1. 打开项目的“属性页” 对话框。Open the project's Property Pages dialog box.

  2. 单击“引用路径” 属性页。Click the References Path property page.

  3. 修改列表框的内容。Modify the contents of the list box.

有关如何以编程方式设置此编译器选项的信息,请参阅 ReferencePathFor information on how to set this compiler option programmatically, see ReferencePath.


编译 t2.cs 以创建 .exe 文件。Compile t2.cs to create an .exe file. 编译器将在工作目录和 C 驱动器的根目录中查找程序集引用。The compiler will look in the working directory and in the root directory of the C drive for assembly references.

csc -lib:c:\ -reference:t2.dll t2.cs  

请参阅See also