Export (0) Print
Expand All

Compiling a Native C++ Program from the Command Line (C++)

Updated: July 2009

Visual C++ includes a C++ compiler that you can use to create everything from simple Visual C++ programs to Windows Forms applications and components.

In this procedure, you create simple Visual C++ programs by using a text editor and compile them on the command line.

You can also compile Visual C++ programs that you created with a text editor by using the Visual Studio development environment. See Compiling a C++ Program that Targets the CLR in Visual Studio (C++) for more information.

You can use your own Visual C++ programs instead of typing the simple programs shown in these procedures. You can also use any of the Visual C++ code sample programs in the help topics.

These topics assume that you understand the fundamentals of the C++ language.

To create a Visual C++ source file and compile it on the command line

  1. Open the Visual Studio 2008 Command Prompt window.

    Click the Start button, point to All Programs, Microsoft Visual Studio 2008, Visual Studio Tools, and then click Visual Studio 2008 Command Prompt.

    Depending on your operating system and configuration, you may need to open the Visual Studio 2008 Command Prompt with Administrator privileges to compile the code successfully using the steps described in this topic. To do this, right-click on Visual Studio 2008 Command Prompt and select Run as administrator. Then set the following directory as the current directory at the command prompt: \Program Files\Microsoft Visual Studio <version>\VC.

    NoteNote:

    The Visual Studio 2008 Command Prompt automatically sets up the correct path of the Visual C++ compiler and any needed libraries. Use it instead of the regular Command Prompt window. For more information, see Setting the Path and Environment Variables for Command-Line Builds.

  2. At the command prompt, type notepad simple.cpp and press Enter.

    Click Yes when you are prompted to create a new file.

  3. In Notepad, type the following lines:

    #include <iostream>
    
    int main()
    {
        std::cout << "This is a native C++ program." << std::endl;
        return 0;
    }
    
  4. On the File menu, click Save. You have created a Visual C++ source file.

  5. On the File menu, click Exit to close Notepad.

  6. At the command prompt, type cl /EHsc simple.cpp and press Enter. The /EHsc command-line option instructs the compiler to enable C++ exception handling. For more information, see /EH (Exception Handling Model).

    The cl.exe compiler generates an executable program simple.exe.

    You can see the executable program name in the lines of output information that the compiler displays.

  7. To see a list of all files in the directory named simple with any extension, type dir simple.* and press Enter.

    The .obj file is an intermediate format file that you can safely ignore.

  8. To run the simple.exe program, type simple and press Enter.

    The program displays this text and exits:

    This is a native C++ program.

  9. To close the Command Prompt window, type exit and press Enter.

This procedure shows the command line that you use to compile a Visual C++ program that uses .NET Framework classes.

You must use the /clr (Common Language Runtime Compilation) compiler option because this program uses .NET classes and requires the Visual C++ compiler to include the necessary .NET libraries. The Visual C++ compiler generates an .exe file that contains MSIL code instead of machine executable instructions.

You can follow the steps in this procedure to compile any sample Visual C++ program in the help topics.

To compile a Visual C++ .NET console application on the command line

  1. Open the Visual Studio 2008 Command Prompt window.

    Click the Start button, point to All Programs, Microsoft Visual Studio 2008, Visual Studio Tools, and click Visual Studio 2008 Command Prompt.

    Depending on your operating system and configuration, you may need to open the Visual Studio 2008 Command Prompt with Administrator privileges to compile the code successfully using the steps described in this topic. To do this, right-click on Visual Studio 2008 Command Prompt and select Run as administrator. Then set the following directory as the current directory at the command prompt: \Program Files\Microsoft Visual Studio <version>\VC.

  2. At the command prompt, type notepad simpleclr.cpp and press Enter.

    Click Yes when you are prompted to create a new file.

  3. In Notepad, type the following lines:

    int main()
    {
        System::Console::WriteLine("This is a Visual C++ program.");
    }
    
  4. On the File menu, click Save.

    You have created a Visual C++ source file that uses a .NET class (Console) and is located in the System namespace.

  5. On the File menu, click Exit to close Notepad.

  6. At the command prompt, type cl /clr simpleclr.cpp and press Enter. The cl.exe compiler generates an executable program simpleclr.exe.

  7. To see a list of all files in the directory named simpleclr with any extension, type dir simpleclr.* and press Enter.

    The .obj file is an intermediate format file that you can safely ignore.

    The .manifest file is an XML file that contains information about the assembly. (An assembly is the .NET unit of deployment, such as an .exe program or .dll component or library.)

  8. To run the simpleclr.exe program, type simpleclr and press Enter.

    The program displays this text and exits:

    This is a Visual C++ program.

  9. To close the Command Prompt window, type exit and press Enter.

Date

History

Reason

July 2009

Added more information on opening the command line with.

Customer feedback.

Community Additions

ADD
Show:
© 2015 Microsoft