Skip to content

Commit faecabc

Browse files
authored
Merge pull request #3239 from MicrosoftDocs/master
10/26/2020 AM Publish
2 parents bf54b40 + bbb50d3 commit faecabc

File tree

3 files changed

+119
-100
lines changed

3 files changed

+119
-100
lines changed
Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,85 @@
11
---
2-
title: "/clr (Common Language Runtime Compilation)"
3-
ms.date: "05/16/2019"
2+
title: "/clr (Common Language Runtime compilation)"
3+
description: "Use the Microsoft C++ compiler option /clr to compile C++/CLI and C++ code as managed code."
4+
ms.date: 10/25/2020
45
f1_keywords: ["/CLR", "VC.Project.VCNMakeTool.CompileAsManaged", "VC.Project.VCCLCompilerTool.CompileAsManaged"]
56
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"]
67
ms.assetid: fec5a8c0-40ec-484c-a213-8dec918c1d6c
78
---
8-
# /clr (Common Language Runtime Compilation)
9+
# `/clr` (Common Language Runtime Compilation)
910

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

1213
## Syntax
1314

14-
> **/clr**[**:**_options_]
15+
> **`/clr`**\[**`:`**_options_]
1516
1617
## Arguments
1718

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

2122
- none
2223

23-
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).
24+
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).
2425

25-
- **pure**
26+
- **`pure`**
2627

27-
**/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#.
28+
**`/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#.
2829

29-
- **safe**
30+
- **`safe`**
3031

31-
**/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#.
32+
**`/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#.
3233

33-
- **noAssembly**
34+
- **`noAssembly`**
3435

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

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

39-
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.
40+
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.
4041

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

43-
- **initialAppDomain**
44+
- **`initialAppDomain`**
4445

45-
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.
46+
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.
4647

47-
- **nostdlib**
48+
- **`nostdlib`**
4849

49-
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.
50+
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.
5051

5152
## Remarks
5253

53-
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).
54+
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).
5455

55-
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).
56+
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).
5657

57-
An application compiled by using **/clr** may or may not contain managed data.
58+
An application compiled by using **`/clr`** may or may not contain managed data.
5859

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

61-
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).
62+
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).
6263

63-
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.
64+
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.
6465

65-
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).
66+
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.
6667

67-
**/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).
68+
**`/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).
6869

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

71-
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**).
72+
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`**).
7273

73-
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.
74+
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.
7475

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

77-
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.
78+
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.
7879

79-
## Metadata and Unnamed Classes
80+
### Metadata and Unnamed Classes
8081

81-
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.
82+
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.
8283

8384
```cpp
8485
// clr_unnamed_class.cpp
@@ -88,7 +89,21 @@ class {} x;
8889

8990
Use ildasm.exe to view metadata.
9091

92+
### To set this compiler option in the Visual Studio development environment
93+
94+
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).
95+
96+
1. Set the **Configuration** dropdown to **All configurations**, and set the **Platform** dropdown to **All Platforms**.
97+
98+
1. Select the **Configuration Properties** > **Advanced** page.
99+
100+
1. Modify the **Common Language Runtime Support** property. Choose **OK** to save your changes.
101+
102+
### To set this compiler option programmatically
103+
104+
- See <xref:Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool.CompileAsManaged>.
105+
91106
## See also
92107

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

0 commit comments

Comments
 (0)