MSDN Library
Send Community Content Click to Rate and Give Feedback
Give feedback on this content
Collapse All Expand All Language Filter : AllVisual Basic
C#
C++
J#
JScript
XAML
F#
Visual C++
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.
Prerequisites
These topics assume that you understand the fundamentals of the C++ language. If you are just getting started learning C++, we recommend the "C++ Beginner's Guide," written by Herb Schildt, available online at
http://go.microsoft.com/fwlink/?LinkId=115303.
To create a Visual C++ source file and compile it on the command line
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.
Note:
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.
At the command prompt, type notepad simple.cpp and press Enter.
Click Yes when you are prompted to create a new file.
In Notepad, type the following lines:
Copy Code
#include <iostream>
int main()
{
std::cout << "This is a native C++ program." << std::endl;
return 0;
}
On the File menu, click Save. You have created a Visual C++ source file.
On the File menu, click Exit to close Notepad.
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.
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.
To run the simple.exe program, type simple and press Enter.
The program displays this text and exits:
This is a native C++ program.
To close the Command Prompt window, type exit and press Enter.
Compiling a Visual C++ Program That Uses .NET Classes
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
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.
At the command prompt, type notepad simpleclr.cpp and press Enter.
Click Yes when you are prompted to create a new file.
In Notepad, type the following lines:
Copy Code
int main()
{
System::Console::WriteLine("This is a Visual C++ program.");
}
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.
On the File menu, click Exit to close Notepad.
At the command prompt, type cl /clr simpleclr.cpp and press Enter. The cl.exe compiler generates an executable program simpleclr.exe.
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.)
To run the simpleclr.exe program, type simpleclr and press Enter.
The program displays this text and exits:
This is a Visual C++ program.
To close the Command Prompt window, type exit and press Enter