Skip to content

Repo sync for protected CLA branch #2555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 26, 2020
87 changes: 51 additions & 36 deletions docs/build/reference/clr-common-language-runtime-compilation.md
Original file line number Diff line number Diff line change
@@ -1,84 +1,85 @@
---
title: "/clr (Common Language Runtime Compilation)"
ms.date: "05/16/2019"
title: "/clr (Common Language Runtime compilation)"
description: "Use the Microsoft C++ compiler option /clr to compile C++/CLI and C++ code as managed code."
ms.date: 10/25/2020
f1_keywords: ["/CLR", "VC.Project.VCNMakeTool.CompileAsManaged", "VC.Project.VCCLCompilerTool.CompileAsManaged"]
helpviewer_keywords: ["cl.exe compiler, common language runtime option", "-clr compiler option [C++]", "clr compiler option [C++]", "/clr compiler option [C++]", "Managed Extensions for C++, compiling", "common language runtime, /clr compiler option"]
ms.assetid: fec5a8c0-40ec-484c-a213-8dec918c1d6c
---
# /clr (Common Language Runtime Compilation)
# `/clr` (Common Language Runtime Compilation)

Enables applications and components to use features from the common language runtime (CLR).

## Syntax

> **/clr**[**:**_options_]
> **`/clr`**\[**`:`**_options_]

## Arguments

*options*<br/>
One or more of the following switches, comma-separated.
*options*\
One or more of the following comma-separated arguments.

- none

With no options, **/clr** creates metadata for the application. 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](../../dotnet/mixed-native-and-managed-assemblies.md).
With no options, **`/clr`** creates metadata for the application. 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](../../dotnet/mixed-native-and-managed-assemblies.md).

- **pure**
- **`pure`**

**/clr:pure is deprecated**. The option is removed in Visual Studio 2017 and later. We recommend that you port code that must be pure MSIL to C#.
**`/clr:pure` is deprecated**. The option is removed in Visual Studio 2017 and later. We recommend that you port code that must be pure MSIL to C#.

- **safe**
- **`safe`**

**/clr:safe is deprecated**. The option is removed in Visual Studio 2017 and later. We recommend that you port code that must be safe MSIL to C#.
**`/clr:safe` is deprecated**. The option is removed in Visual Studio 2017 and later. We recommend that you port code that must be safe MSIL to C#.

- **noAssembly**
- **`noAssembly`**

**/clr:noAssembly is deprecated**. Use [/LN (Create MSIL Module)](ln-create-msil-module.md) instead.
**`/clr:noAssembly` is deprecated**. Use [`/LN` (Create MSIL Module)](ln-create-msil-module.md) instead.

Specifies that an assembly manifest should not be inserted into the output file. By default, the **noAssembly** option is not in effect.
Tells the compiler not to insert an assembly manifest into the output file. By default, the **`noAssembly`** option isn't in effect.

A managed program that does not have assembly metadata in the manifest is known as a *module*. The **noAssembly** option can be used only to produce a module. If you compile by using [/c](c-compile-without-linking.md) and **/clr:noAssembly**, then specify the [/NOASSEMBLY](noassembly-create-a-msil-module.md) option in the linker phase to create a module.
A managed program that doesn't have assembly metadata in the manifest is known as a *module*. The **`noAssembly`** option can be used only to produce a module. If you compile by using [`/c`](c-compile-without-linking.md) and **`/clr:noAssembly`**, then specify the [`/NOASSEMBLY`](noassembly-create-a-msil-module.md) option in the linker phase to create a module.

Before Visual Studio 2005, **/clr:noAssembly** required **/LD**. **/LD** is now implied when you specify **/clr:noAssembly**.
Before Visual Studio 2005, **`/clr:noAssembly`** required **`/LD`**. **`/LD`** is now implied when you specify **`/clr:noAssembly`**.

- **initialAppDomain**
- **`initialAppDomain`**

Enables a Visual C++ application to run on version 1 of the CLR. 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.
Enables a C++/CLI application to run on version 1 of the CLR. An application that's compiled by using **`initialAppDomain`** shouldn't be used by an application that uses ASP.NET because it's not supported in version 1 of the CLR.

- **nostdlib**
- **`nostdlib`**

Instructs the compiler to ignore the default \clr directory. 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.
Instructs the compiler to ignore the default *`\clr`* directory. The compiler produces errors if you include multiple versions of a DLL, such as System.dll. This option lets you specify the specific framework to use during compilation.

## Remarks

Managed code is code that can be inspected and managed by the CLR. Managed code can access managed objects. For more information, see [/clr Restrictions](clr-restrictions.md).
Managed code is code that can be inspected and managed by the CLR. Managed code can access managed objects. For more information, see [`/clr `Restrictions](clr-restrictions.md).

For information about how to develop applications that define and consume managed types, see [Component Extensions for Runtime Platforms](../../extensions/component-extensions-for-runtime-platforms.md).
For information about how to develop applications that define and consume managed types in C++, see [Component Extensions for Runtime Platforms](../../extensions/component-extensions-for-runtime-platforms.md).

An application compiled by using **/clr** may or may not contain managed data.
An application compiled by using **`/clr`** may or may not contain managed data.

To enable debugging on a managed application, see [/ASSEMBLYDEBUG (Add DebuggableAttribute)](assemblydebug-add-debuggableattribute.md).
To enable debugging on a managed application, see [`/ASSEMBLYDEBUG` (Add DebuggableAttribute)](assemblydebug-add-debuggableattribute.md).

Only CLR types will be instantiated on the garbage-collected heap. For more information, see [Classes and Structs](../../extensions/classes-and-structs-cpp-component-extensions.md). To compile a function to native code, use the `unmanaged` pragma. For more information, see [managed, unmanaged](../../preprocessor/managed-unmanaged.md).
Only CLR types are instantiated on the garbage-collected heap. For more information, see [Classes and Structs](../../extensions/classes-and-structs-cpp-component-extensions.md). To compile a function to native code, use the `unmanaged` pragma. For more information, see [`managed`, `unmanaged`](../../preprocessor/managed-unmanaged.md).

By default, **/clr** is not in effect. When **/clr** is in effect, **/MD** is also in effect. For more information, see [/MD, /MT, /LD (Use Run-Time Library)](md-mt-ld-use-run-time-library.md). **/MD** ensures that the dynamically linked, multithreaded versions of the runtime routines are selected from the standard header (.h) files. Multithreading is required for managed programming because the CLR garbage collector runs finalizers in an auxiliary thread.
By default, **`/clr`** isn't in effect. When **`/clr`** is in effect, **`/MD`** is also in effect. For more information, see [`/MD`, `/MT`, `/LD` (Use Run-Time Library)](md-mt-ld-use-run-time-library.md). **`/MD`** ensures that the dynamically linked, multithreaded versions of the runtime routines are selected from the standard header files. Multithreading is required for managed programming because the CLR garbage collector runs finalizers in an auxiliary thread.

If you compile by using **/c**, you can specify the CLR type of the resulting output file with [/CLRIMAGETYPE](clrimagetype-specify-type-of-clr-image.md).
If you compile by using **`/c`**, you can specify the CLR type of the resulting output file by using the [`/CLRIMAGETYPE`](clrimagetype-specify-type-of-clr-image.md) linker option.

**/clr** implies **/EHa**, and no other **/EH** options are supported for **/clr**. For more information, see [/EH (Exception Handling Model)](eh-exception-handling-model.md).
**`/clr`** implies **`/EHa`**, and no other **`/EH`** options are supported for **`/clr`**. For more information, see [`/EH` (Exception Handling Model)](eh-exception-handling-model.md).

For information about how to determine the CLR image type of a file, see [/CLRHEADER](clrheader.md).
For information about how to determine the CLR image type of a file, see [`/CLRHEADER`](clrheader.md).

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**).
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`**).

Use the [/ASSEMBLYRESOURCE](assemblyresource-embed-a-managed-resource.md) linker option to embed a resource in an assembly. [/DELAYSIGN](delaysign-partially-sign-an-assembly.md), [/KEYCONTAINER](keycontainer-specify-a-key-container-to-sign-an-assembly.md), and [/KEYFILE](keyfile-specify-key-or-key-pair-to-sign-an-assembly.md) linker options also let you customize how an assembly is created.
Use the [`/ASSEMBLYRESOURCE`](assemblyresource-embed-a-managed-resource.md) linker option to embed a resource in an assembly. [`/DELAYSIGN`](delaysign-partially-sign-an-assembly.md), [`/KEYCONTAINER`](keycontainer-specify-a-key-container-to-sign-an-assembly.md), and [`/KEYFILE`](keyfile-specify-key-or-key-pair-to-sign-an-assembly.md) linker options also let you customize how an assembly is created.

When **/clr** is used, the `_MANAGED` symbol is defined to be 1. For more information, see [Predefined Macros](../../preprocessor/predefined-macros.md).
When **`/clr`** is used, the `_MANAGED` symbol is defined to be 1. For more information, see [Predefined macros](../../preprocessor/predefined-macros.md).

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](../../preprocessor/init-seg.md) only affects the order of initialization in the managed and unmanaged categories.
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`](../../preprocessor/init-seg.md) only affects the order of initialization in the managed and unmanaged categories.

## Metadata and Unnamed Classes
### Metadata and Unnamed Classes

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.
Unnamed classes appear in metadata under names such as `$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.

```cpp
// clr_unnamed_class.cpp
Expand All @@ -88,7 +89,21 @@ class {} x;

Use ildasm.exe to view metadata.

### To set this compiler option in the Visual Studio development environment

1. Open the project's **Property Pages** dialog box. For details, see [Set C++ compiler and build properties in Visual Studio](../working-with-project-properties.md).

1. Set the **Configuration** dropdown to **All configurations**, and set the **Platform** dropdown to **All Platforms**.

1. Select the **Configuration Properties** > **Advanced** page.

1. Modify the **Common Language Runtime Support** property. Choose **OK** to save your changes.

### To set this compiler option programmatically

- See <xref:Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool.CompileAsManaged>.

## See also

[MSVC Compiler Options](compiler-options.md)<br/>
[MSVC Compiler Options](compiler-options.md)\
[MSVC Compiler Command-Line Syntax](compiler-command-line-syntax.md)
Loading