Programming in C++ on Visual Studio



Programming in C++ with Visual (unmanaged code)

1) Using the Command-Line

Let’s compile a simple program like hello.cpp (shown below) from the command line.

#include

using namespace std;

int main()

{

cout Programs -> Microsoft Visual Studio .NET -> Visual Studio .NET Tools -> Command Prompt

 

Change the directory to the appropriate folder in which you wish to code:

C:\>cd myFolder\example1

 

We can then compile and link using ...

C:\myFolder\example1>cl /D "WIN32" /D "_CONSOLE" /EHsc /MLd /W3 /nologo hello.cpp

 

There should now be a file named hello.exe in the example1 directory. To run this executable, just enter its name:

C:\myFolder\example1>hello.exe 

 

You can leave out the .exe bit as well.

Those options on the compile and link command (cl) are pretty horrible, so it might be a good idea to build a batch file. Create the file compile.bat that contains the lines:

echo off

cl /D "WIN32" /D "_CONSOLE" /EHsc /MLd /W3 /nologo %1

Then you can use:

C:\myFolder\example1>compile hello.cpp

to compile and link your code.

2) Using the Visual IDE

To fire up Visual , use the menu options:

 

Start -> Programs -> Microsoft Visual Studio .NET -> Microsoft Visual Studio .NET

 

This will start up , and present you with the start page:

[pic]

Either click on the New Project button, or select the menu options:

File -> New -> Project

 

then displays the New Project dialog box:

[pic]

Select Visual C++ Projects as the Project Type, Win32 Console Project as the Template, and choose an appropriate name for the project. Also navigate to a suitable location - the project will be placed in this folder.

 A Wizard will appear to further help you set up the application:

  

[pic]

 

Click on the Application Settings option to bring up another dialog:

[pic]

Select the Console Application and Empty Project options, and click on Finish. An empty VC++ solution will then be made available.

(Note: you can get rid of the Start Page if you wish - just click on the X on the upper right of the Start Page component.)

[pic]

 

In the Solution Explorer window (to the upper right), there will be folders present for .cpp source files, and .h header files, but with no files present. Let’s add a simple .cpp source file. Select the menu items:

Project -> Add New Item

[pic]

 

(Note: another way of doing this is to right click on the project (HelloWorld) in the Solution Explorer window, then click on the Add -> Add New Item menu selections.)

 

[pic]

 

This will bring up the Add New Item dialog window:

[pic]

Select C++ File (.cpp) as the template type, and select a suitable name for the .cpp file. Notice also that you can add header (.h) files in the same manner. Click on the Open button. An editor window will appear for you to enter your source code. For example, enter the code as shown below:

[pic]

 

To compile, link and execute this code, you can either click on the Start Button [pic], press F5, or use the

Debug -> Start

menu options. VC++ will first ask permission to compile your code:

[pic]

 

Just click on the Yes button.

If you have no errors, the code will be compiled, linked and executed. A black DOS window will appear, then disappear instantly. To hold the window in order to view its contents, add the line:

cin.get();

as your final statement. This will cause the DOS window to hang, pending input from the user. Just hit Enter to close off the DOS window.

[pic]

Run again to get the expected output:

[pic]

An alternative is to use Ctrl-F5 (Start without debugging), which automatically hangs the DOS window for you:

[pic]

Again, just hit enter to shut down the DOS window.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download