138 out of 244 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?
(2000 characters remaining)
Community Content Add
Annotations FAQ
Does work on Vista
After reading a comment about not working on vista I immediately went to the properties of the program and ran it in compatable mode with Windows XP. This method of compiling then worked for me. I never tried it before running it for Windows XP though so I'm not sure if it would have.
Error! cl simple.cpp
If I compile it in the "Microsoft Visual Studio 9.0\VC" directory I get the message that the program is too big to fin in memory (in german "Programm zu groß für den Arbeitsspeicher")
If I compile it outside of the ..VC directory (after all, envars with a path to cl should be set correctly) i get a message in the message box (!) that its not a proper Win32 application!

[tfl - 03 10 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
SQL Server :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
.NET Framework :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
Errors when I try to do the cl /clr example

Original error (using VS2003 command prompt):

simpleclr.cpp
simpleclr.cpp(3) : error C2653: 'System' : is not a class or namespace name
simpleclr.cpp(3) : error C3861: 'WriteLine': identifier not found, even with argument-dependent lookup

--

Jun 2009: Tried this with VS2005 command prompt and it works.

Needs to be updated to work with Vista

Doesn't work in Vista as is. You need to give your user account sufficient priviledges to write data on the VC folder or modify the environment path to add somewhere you do have permission to write to.

Advertisement