26 out of 38 rated this helpful - Rate this topic

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.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Command line DLL Compile
You can build DLLs using

cl -o myhook.dll myHook.cpp /D MYHOOK_DLLEXPORT /link /DLL /DEF:"MyHook.def"

Where MyHook.def is the definition file that contains the exported functions. You need this for explicit linking via GetProcAddress.
Issue with Vista

I found another easier way to run it IMHO. You right click on the VC cmd prompt and run as admin. From there you:

cd Program Files (x86)\Microsoft Visual Studio 9.0\VC

then you can run as they stated:

cl /EHsc simple.cpp

I did not even know about this feature when I looked into Visual Studio. It's nice to not have to start a new project to compile a small source file etc.

Consult message above.
My apologies for the double post; it seems my Visual C++ was malfunctioning, as the script couldn't load. Please consult my original post (the post above this one) and consider replying, for your help is currently being sought. :)
Lack of comprehension :| Fatal Error? Say, what on Earth does this mean?
How come it doesn't work on mine? I'm on Vista too, however, each time I attempt the "cl /EHsc" command the compiler returns an error message: LINK : fatal error LNK1104: cannot open file 'kernel32.lib' I must admit I do not understand how this compiler works very much, having been used to simpler GUI-based compilers such as Dev-C++ (which, quite unfortunately, did not work well with all of my codes, thus leading me to make the switch to Microsoft Visual C++, a program recommended to me by an amateur programmer. I am currently not in touch with the programmer, though, and as a result, I have took it upon myself to find the answers in this site. Hopefully you will supply me with answers to my predicament soon, as I am doing this for a school requirement and I shall not pass this course if I do not submit my programs by the 14th. Many thanks.
This can work on Vista

On Vista, it will work. "Program Files" is not writable by default, so at command line:

C:\Program Files\Microsoft Visual Studio 9.0\VC>cd %userprofile%\Documents

C:\Users\USERNAME\Documents>notepad simpleclr.cpp

< Write program, Save, and Exit Notepad >

C:\Users\USERNAME\Documents>cl /clr simpleclr.cpp

< Compiling >

C:\Users\USERNAME\Documents>simpleclr.exe

This is a Visual C++ program using CLR.

Substitute /clr for /EHsc to use Exception handling without .NET Framework

help
This doesn't work for Vista
Advertisement