Now that we have learned about the Visual Studio IDE, we are ready
to start writing programs with Visual C++. The first types of
application we will learn to create are command-line applications. A
command-line application does not contain a graphical user interface
(GUI). Typically, command-line applications read input from the
console and write output to the console instead of to a graphical
window.
In this section you will learn how to create both C and
C++ command-line applications. You will also learn how to create
standard C and C++ programs that do not use Microsoft extensions.
This is useful if you want to use Visual C++ to create applications
for use on other operating systems.
Prerequisites
These topics assume that you understand the fundamentals
of the C++ language.
In
This Section
Creating a Standard C++ Program (C++)
Compiling a Native C++ Program from the Command Line
(C++)
Compiling a C++ Program that Targets the CLR in Visual
Studio (C++)
Compiling a C Program
Creating a Standard C++ Program (C++)
With Visual C++ 2008, you can create Standard C++ programs by using
the Visual Studio Development Environment. In this procedure, you
create a new project, add a new file to the project, modify the file
to add C++ code, and then compile and run the program using Visual
Studio.
You can type your own C++ program or use one of the sample
programs. The sample program that we use in this procedure is a
console application. This application uses the set container
in the Standard Template Library (STL), which is part of the ISO C++
98 standard.
Visual C++ complies with these standards:
ISO C 95
ISO C++ 98
Ecma C++/CLI 05
Note:
You should use the /Za compiler option if
you want to enforce ANSI C++ and ANSI C compliance
checking of your program (the default option is /Ze
which allows Microsoft extensions to the standard).
See
/Za, /Ze (Disable Language Extensions) for more
information.
To create a new project and add a source
file
Create a new project:
On the File
menu, point to New, and then click
Project�.
From the Visual C++ project types,
click Win32, and then click
Win32 Console Application.
Enter a project name.
By default, the solution that
contains the project has the same name as the new project, but
you can enter a different name. You can enter a different
location for the project if you want.
Click OK to create the new project.
In the Win32 Application Wizard,
select Empty Project and click
Finish.
If Solution Explorer is not visible, click
Solution Explorer on the View
menu.
Add a new source file to the project:
Right-click on the Source Files
folder in Solution Explorer, point to Add,
and click New Item.
Click C++ File (.cpp) from the
Code node, enter a file name, and
then click Add.
The .cpp file appears in the Source Files folder in
Solution Explorer and a tabbed window appears where you type in
the code.
Click in the newly created tab in Visual Studio and type a
valid C++ program that uses the Standard C++ Library, or copy
and paste one of the sample programs.
For example, you can use
the
set::find (STL Samples) sample program in the
Standard Template Library Samples topics
in the help.
If you use the sample program for this procedure, notice the
using namespace std; directive. This
directive allows the program to use cout and endl
without requiring fully qualified names (std::cout and
std::endl).
On the Build menu, click
Build Solution.
The
Output window displays information about the compilation
progress, such as the location of the build log and a message
that indicates the build status.
On the Debug menu, click
Start without Debugging.
If you used
the sample program, a command window is displayed that shows
whether certain integers are found in the set.
Compiling a Native C++ Program from the Command Line (C++)
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.
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.
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
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.
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.
Compiling a C++ Program that Targets the CLR in Visual Studio (C++)
You can create Visual C++ programs that use .NET classes and compile
them by using the Visual Studio Development Environment.
For this
procedure you can type your own Visual C++ program or use one of the
sample programs. The sample program that we use in this procedure
creates a text file named textfile.txt and saves it to the project
directory.
To create a new project in Visual Studio
and add a new source file
Create a new project. On the File
menu, point to New, and then click
Project�.
From the Visual C++ project types, click
CLR, and then click CLR Empty Project.
Type a project name.
By default, the solution that
contains the project has the same name as the new project, but
you can enter a different name. You can enter a different
location for the project if you want.
Click OK to create the new project.
If Solution Explorer is not visible, click
Solution Explorer on the View
menu.
Add a new source file to the project:
Right-click the Source Files
folder in Solution Explorer, point to Add
and click New Item�.
Click C++ File (.cpp) and type a
file name and then click Add.
The .cpp file appears in the Source
Files folder in Solution Explorer and a tabbed window
appears where you type the code you want in that file.
Click in the newly created tab in Visual Studio and type a
valid Visual C++ program, or copy and paste one of the sample
programs.
For example, you can use the
How to: Write a Text File sample program (in the
File Handling and I/O node of the
Programming Guide).
If you use the sample program, notice that you use the
gcnewkeyword instead ofnewwhen
creating a .NET object, and thatgcnewreturns
a handle (^) rather than a pointer (*):
StreamWriter^ sw = gcnew
StreamWriter(fileName);
For more information on the new Visual C++ syntax, see
Language Features for Targeting the CLR.
On the Build menu, click
Build Solution.
The
Output window displays information about the compilation
progress, such as the location of the build log and a message
that indicates the build status.
If you make changes and run the program without doing a
build, a dialog box might indicate that the project is out of
date. Select the checkbox on this dialog before you click
OK if you want Visual Studio to always
use the current versions of files instead of prompting you each
time it builds the application.
On the Debug menu, click
Start without Debugging.
If you used the sample program, when you run the program a
command window is displayed that indicates the text file has
been created. Press any key to close the command window.
The
textfile.txt text file is now located in your project
directory. You can open this file by using Notepad.
Note:
Choosing the empty CLR project template
automatically set the /clr compiler option.
To verify this, right-click the project in
Solution Explorer and
clicking Properties, and
then check the Common Language
Runtime support option in the
General node of
Configuration Properties.
Compiling a C Program
Visual C++ 2008 includes a C compiler that you can use to create
everything from simple Visual C programs to Windows API
applications.
In this procedure, you create a simple Visual C
program by using a text editor and compile it on the command line.
You can use your own Visual C programs instead of typing the
simple programs shown in these procedures. You can also use any
Visual C code sample programs that are included in the help topics.
By default, the Visual C++ compiler treats all files that end in
.c as C source code, and all files that end in .cpp as C++ source
code. To force the compiler to treat all files as C regardless of
extension, use the
/Tc compiler option.
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.
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.c and
press Enter.
Click
Yes when you are prompted to create a new file.
In Notepad, type the following lines:
Copy Code
#include
int main()
{
printf("This is a native C program.\n");
return 0;
}
From the File menu, select
Save. You have created a Visual C source
file.
From the File menu, select
Exit to close Notepad.
At the command prompt, type cl simple.c and press
Enter.
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:
Copy Code
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
simple.c
Microsoft (R) Incremental Linker Version 9.00
Copyright (C) Microsoft Corporation. All rights reserved.
/out:simple.exe
simple.obj
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.