Anda di halaman 1dari 5

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

Page 1 of 5

How to: Compile a Native C++ Program from the Command Line
Visual Studio 2005 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 Studio1 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 Topics2 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. Note 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 Builds3. 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> intmain() { std::cout<<"ThisisanativeC++program."<<std::endl; return0; }

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.

http://msdn.microsoft.com/en-us/library/ms235639(d=printer,v=vs.80).aspx

5/12/2011

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

Page 2 of 5

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)4. 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)5 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 Topics2 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:

http://msdn.microsoft.com/en-us/library/ms235639(d=printer,v=vs.80).aspx

5/12/2011

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

Page 3 of 5

4. On the File menu, click Save. You have created a Visual C++ source file using a .NET class (Console6) that is located in the System7 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
Other Resources Visual C++ Guided Tour8 C++ Language Reference9 Building a C/C++ Program10

Links Table
1 2 3 4 5 6 7 8

http://msdn.microsoft.com/en-us/library/ms235635(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/ms235637(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/f2ccy3wt(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/1deeycx5(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/k8d11d4s(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/system.console(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/system(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/ms235630(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/ms235639(d=printer,v=vs.80).aspx

5/12/2011

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

Page 4 of 5

http://msdn.microsoft.com/en-us/library/3bstk3k5(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/z7kx322x(v=vs.80).aspx

10

Community Content
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.

3/16/2011 zoocows

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 fr 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&

10/4/2009 Thomas Lee

9/22/2009 sonicth

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 argumentdependent lookup

http://msdn.microsoft.com/en-us/library/ms235639(d=printer,v=vs.80).aspx

5/12/2011

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

Page 5 of 5

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

6/2/2009 AS-MSDN

2/5/2007 AS-MSDN

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.

9/8/2007 BLiTZWiNG

2011 Microsoft. All rights reserved.

http://msdn.microsoft.com/en-us/library/ms235639(d=printer,v=vs.80).aspx

5/12/2011