Export (0) Print
Expand All
154 out of 269 rated this helpful - Rate this topic

How to: Compile a Native C++ Program from the Command Line 

Visual C++ includes a C++ compiler 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 with a text editor and compile them on the command line.

You can also compile Visual C++ programs you create with a text editor using the Visual Studio development environment. See How to: Compile a C++ Program that Targets the CLR in Visual Studio for more information.

You can use your own Visual C++ programs instead of typing in the simple programs shown in these procedures. You can also use any of the Visual C++ code sample programs included in the help topics. See How to: Compile a Code Example from the Help Topics for more information.

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

  1. Open the Visual Studio 2005 Command Prompt window.

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

    NoteNote

    The Visual Studio 2005 Command Prompt automatically sets up the correct path to the Visual C++ compiler and any needed libraries, so it is used 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 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 line 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 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 you can 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.

Compiling a Visual C++ Program with .NET Classes

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

The /clr (Common Language Runtime Compilation) compiler option is necessary 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 that contains MSIL code rather than machine executable instructions.

You can use the steps in this procedure to compile any of the sample Visual C++ programs included in the help topics. For example, the How to: Compile a Code Example from the Help Topics topic refers to a sample program that uses .NET classes to calculate the amount of time since the last Windows start-up occurred.

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

  1. Open the Visual Studio 2005 Command Prompt window.

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

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

    Click Yes when 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 using a .NET class (Console) that is located in the System namespace.

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

  6. At the command line prompt, type cl /clr simpleclr.cpp and press Enter:

  7. The cl.exe compiler generates an executable program simpleclr.exe.

  8. 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 you can 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).

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

    The program displays this text and exits:

    This is a Visual C++ program.

  10. To close the command prompt window, type exit and press Enter.

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.