/clr(公共语言运行时编译)/clr (Common Language Runtime Compilation)

允许应用程序和组件使用公共语言运行时 (CLR) 中的功能。Enables applications and components to use features from the common language runtime (CLR).


/clr[ : options]/clr[:options]


下列开关中的一个或多个(以逗号分隔)。One or more of the following switches, comma-separated.

  • none

    不带选项,/clr 为应用程序创建元数据。With no options, /clr creates metadata for the application. 其他 CLR 应用程序可以使用该元数据,并且该应用程序也可以使用其他 CLR 组件的元数据中的类型和数据。The metadata can be consumed by other CLR applications, and enables the application to consume types and data in the metadata of other CLR components. 有关详细信息,请参阅混合(本机和托管)程序集For more information, see Mixed (Native and Managed) Assemblies.

  • purepure

    /clr:pure 已弃用/clr:pure is deprecated. 在 Visual Studio 2017 及更高版本中,该选项已删除。The option is removed in Visual Studio 2017 and later. 建议移植对 C# 来说必须是纯 MSIL 的代码。We recommend that you port code that must be pure MSIL to C#.

  • 安全safe

    /clr:safe 已弃用/clr:safe is deprecated. 在 Visual Studio 2017 及更高版本中,该选项已删除。The option is removed in Visual Studio 2017 and later. 建议移植对 C# 来说必须是安全 MSIL 的代码。We recommend that you port code that must be safe MSIL to C#.

  • noAssemblynoAssembly

    /clr:noAssembly 已弃用/clr:noAssembly is deprecated. 请改用 /LN (Create MSIL Module)Use /LN (Create MSIL Module) instead.

    指定不应将程序集清单插入输出文件中。Specifies that an assembly manifest should not be inserted into the output file. 默认情况下, noAssembly 选项是无效的。By default, the noAssembly option is not in effect.

    清单中不具有程序集元数据的托管程序称为 “模块”A managed program that does not have assembly metadata in the manifest is known as a module. noAssembly 选项只能用于生成模块。The noAssembly option can be used only to produce a module. 如果使用 /c/clr:noAssembly进行编译,请在创建模块的链接器阶段指定 /NOASSEMBLY 选项。If you compile by using /c and /clr:noAssembly, then specify the /NOASSEMBLY option in the linker phase to create a module.

    在低于 Visual Studio 2005 的版本中,/clr:noAssembly 需要 /LD。Before Visual Studio 2005, /clr:noAssembly required /LD. 现在,指定 /LD 时即暗含 /LD/LD is now implied when you specify /clr:noAssembly.

  • initialAppDomaininitialAppDomain

    允许 Visual C++ 应用程序在 CLR 版本 1 上运行。Enables a Visual C++ application to run on version 1 of the CLR. 使用 initialAppDomain 编译的应用程序不应由使用 ASP.NET 的应用程序使用,因为它在 CLR 版本 1 中不受支持 。An application that is compiled by using initialAppDomain should not be used by an application that uses ASP.NET because it is not supported in version 1 of the CLR.

  • nostdlibnostdlib

    指示编译器忽略默认 \clr 目录。Instructs the compiler to ignore the default \clr directory. 如果包含 DLL 的多个版本(如 System.dll),则编译器将产生错误。The compiler produces errors if you are including multiple versions of a DLL such as System.dll. 使用此选项可指定编译过程中要使用的特定框架。Using this option lets you specify the specific framework to use during compilation.


托管代码是可以由 CLR 检查和管理的代码。Managed code is code that can be inspected and managed by the CLR. 托管代码可以访问托管对象。Managed code can access managed objects. 有关详细信息,请参阅 /clr RestrictionsFor more information, see /clr Restrictions.

有关如何开发定义和使用托管类型的应用程序的信息,请参阅 Component Extensions for Runtime PlatformsFor information about how to develop applications that define and consume managed types, see Component Extensions for Runtime Platforms.

使用 /clr 编译的应用程序可以包含也可以不包含托管数据。An application compiled by using /clr may or may not contain managed data.

若要在托管应用程序上启用调试,请参阅 /ASSEMBLYDEBUG(添加 DebuggableAttribute)To enable debugging on a managed application, see /ASSEMBLYDEBUG (Add DebuggableAttribute).

只有 CLR 类型会在垃圾回收堆上实例化。Only CLR types will be instantiated on the garbage-collected heap. 有关更多信息,请参阅类和结构For more information, see Classes and Structs. 若要将函数编译为本机代码,请使用 unmanaged 杂注。To compile a function to native code, use the unmanaged pragma. 有关详细信息,请参阅托管和非托管For more information, see managed, unmanaged.

默认情况下, /clr 是无效的。By default, /clr is not in effect. /clr 有效时, /MD 也有效。When /clr is in effect, /MD is also in effect. 有关详细信息,请参阅 /MD、/MT、/LD(使用运行时库)For more information, see /MD, /MT, /LD (Use Run-Time Library). /MD 确保从标准头 (.h) 文件中选择动态链接的多线程版本运行时例程。/MD ensures that the dynamically linked, multithreaded versions of the runtime routines are selected from the standard header (.h) files. 托管编程必须进行多线程处理,因为 CLR 垃圾回收器将在辅助线程中运行终结器。Multithreading is required for managed programming because the CLR garbage collector runs finalizers in an auxiliary thread.

如果使用 /c 进行编译,可以通过 /CLRIMAGETYPE 指定生成的输出文件的 CLR 类型。If you compile by using /c, you can specify the CLR type of the resulting output file with /CLRIMAGETYPE.

/clr 暗含 /EHa,且 /clr 不支持任何其他 /EH选项。/clr implies /EHa, and no other /EH options are supported for /clr. 有关详细信息,请参阅 /EH(异常处理模型)For more information, see /EH (Exception Handling Model).

有关如何确定文件的 CLR 映像类型的信息,请参阅 /CLRHEADERFor information about how to determine the CLR image type of a file, see /CLRHEADER.

传递给链接器给定调用的所有模块都必须使用相同的运行时编译器选项( /MD/LD)进行编译。All modules passed to a given invocation of the linker must be compiled by using the same run-time library compiler option (/MD or /LD).

使用 /ASSEMBLYRESOURCE 链接器选项以在程序集中嵌入资源。Use the /ASSEMBLYRESOURCE linker option to embed a resource in an assembly. /DELAYSIGN/KEYCONTAINER/KEYFILE 链接器选项也允许自定义程序集的创建方式。/DELAYSIGN, /KEYCONTAINER, and /KEYFILE linker options also let you customize how an assembly is created.

使用 /clr 时, _MANAGED 符号定义为 1。When /clr is used, the _MANAGED symbol is defined to be 1. 有关更多信息,请参见 Predefined MacrosFor more information, see Predefined Macros.

先初始化本机对象文件中的全局变量(如果可执行文件是 DLL,即在 DllMain 期间进行),再初始化托管部分的全局变量(在运行任何托管代码之前进行)。The global variables in a native object file are initialized first (during DllMain if the executable is a DLL), and then the global variables in the managed section are initialized (before any managed code is run). #pragma init_seg 仅影响托管和非托管类别中的初始化顺序。#pragma init_seg only affects the order of initialization in the managed and unmanaged categories.

元数据和未命名类Metadata and Unnamed Classes

未命名类将出现在按以下方式命名的元数据中: $UnnamedClass$crc-of-current-file-name$index$,其中 index 是未命名类在编译中的顺序计数。Unnamed classes will appear in metadata named as follows: $UnnamedClass$crc-of-current-file-name$index$, where index is a sequential count of the unnamed classes in the compilation. 例如,下面的代码示例将在元数据中生成一个未命名类。For example, the following code sample generates an unnamed class in metadata.

// clr_unnamed_class.cpp
// compile by using: /clr /LD
class {} x;

使用 ildasm.exe 查看元数据。Use ildasm.exe to view metadata.

请参阅See also

MSVC 编译器选项MSVC Compiler Options
MSVC 编译器命令行语法MSVC Compiler Command-Line Syntax