This documentation is archived and is not being maintained.

How to: Compile a C++ Program that Targets the CLR in Visual Studio 

You can create Visual C++ programs that use .NET classes and compile them using the Visual Studio Development Environment.

For this procedure you can type in your own Visual C++ program or use one of the sample programs. The sample program referred to in this procedure creates a text file named textfile.txt and saves it to the project directory.

To create a new project in Visual Studio and add a new source file

  1. Create a new project. On the File menu, point to New, then click Project….

  2. From the Visual C++ project types, click CLR, then click CLR Empty Project.

  3. Enter a project name.

    By default, the solution that contains the project has the same name as the new project, though you can enter a different name. You can enter a different location for the project if you wish.

    Click OK to create the new project.

  4. If Solution Explorer is not visible, click Solution Explorer on the View menu.

  5. Add a new source file to the project:

    • Right-click on the Source Files folder in Solution Explorer and point to Add and click New Item….

    • Click C++ File (.cpp) and enter a file name then click Add.

    The .cpp file appears in the Source Files folder in Solution Explorer and a tabbed window appears where you type the code you want in that file.

  6. Click in the newly created tab in Visual Studio and type in a valid Visual C++ program or copy and paste one of the sample programs.

    For example, you can use the How to: Write a Text File sample program (in the File Handling and I/O node of the Programming Guide). See How to: Compile a Code Example from the Help Topics for information on copying a sample program to the Clipboard.

    If you use the sample program, notice that the new Visual C++ syntax uses the gcnew keyword instead of new when creating a .NET object, and that gcnew returns a handle (^) rather than a pointer (*):

    StreamWriter^ sw = gcnew StreamWriter(fileName);

    For more information on the new Visual C++ syntax, see New Language Features in Visual C++.

  7. On the Build menu, click Build Solution.

    The Output window displays information about the compilation progress, such as the location of the build log and a message indicating that the build succeeded.

    If you make changes and run the program without doing a build, a dialog may appear indicating the project is out of date. Select the checkbox on this dialog before you click OK if you want Visual Studio to always use the current versions of files when building without prompting you.

  8. On the Debug menu, click Start without Debugging.

  9. If you used the sample program, a command window is displayed that indicates the text file has been created. Press any key to close the command window.

    The textfile.txt text file that was created is located in your project directory. You can open this file with Notepad.


    Choosing the empty CLR project template automatically set the /clr compiler option. You can verify this by right-clicking on the project in Solution Explorer and clicking Properties, then checking the Common Language Runtime support option found within the General node of Configuration Properties.

See Also