Visual C .NET 2005 Compiler Part I

Visual C++ .NET 2005 Compiler Part I

A computer can not carry out the instructions in a program without translation into the computer's language: machine language. The program that does this translation is called a compiler. Libraries are added to the program using a linker. After a program has been compiled and linked, it can then be run. This lecture will discuss how to use Microsoft Visual C++.NET 2005 to compile, link and run a simple C++ program. Our objective here is just to learn this process so that later as we write code we can use these techniques on the later programs. What you should notice when using Microsoft Visual .NET 2005 is how much work it does for you. This tool is frequently called an integrated development environment or an IDE. Some people do not like to use IDEs and talk against their use by students. I believe that they are a helpful tool and that you should use an IDE. Among other helpful features, the keywords of C++ are highlighted in blue making easier for you to notice errors. Because of this and other features I believe you should use the IDE and that is the reason for this discussion.

First you will need to start Microsoft Visual Studio .NET using what ever means is on your computer. When you do, you should see a screen similar to the following (except for the projects listed will be different or maybe there will be an entirely different screen):

image from book

Next click on File on the menu bar and then click on the menu option New and then select the Project option. What you should see is like the following:

image from book

After clicking the Project menu option, the New Project dialog box appears and looks like the following:

image from book

On the left in the Project Types section of the dialog box, click on the Visual C++ option and then click on the Win32. In the Templates section click on the Win32 Console Application. I

In the Name box, type the name you will give to the program. For this discussion, type the word: test. When you do this, the name will also appear in the Solution Name box.

For the Location, select C:\Documents and Settings\My Documents\Visual Studio 2005\Projects (or better yet, pick some other location of your choosing or just let Visio Studio .NET use the default location.)

After you have completed these steps, click the OK button. Next should appear the Win32 Application Wizard dialog like the following:

image from book

Notice at the top of the dialog box that Console application is the option. In this course only console projects will be used. If you clicked the Finish button, a partially written Managed C++ program would be written. This will not be the approach in this class. Instead we will be writing Unmanaged C++ programs. Click the Application Settings option on the left side. When you do, you should see a dialog box appear like the following:

image from book

Select Empty project for Additional options and click the checkbox. After these selections have been made, click the Finish button at the bottom.

You should now see a window than contains the Solution Explorer on the left side of the screen. Click this window and then click the pin on the menu bar to fix the Solution Explorer to the screen. When you do, it should look like the following:

image from book

Click a right mouse button on the Source Files option. The following popup should appear:

image from book

Select Add and then New Item.

The Add New Item dialog box should appear like the following. Select the Visual C++ Code option in the Categories window. In the Templates window select: C++ File (.cpp) (.cpp is the extension for C++ source files.) Be sure that test is in the Name text box. (Of course, when you do your homework, other names than test will be selected.) Once these selections have been made, click the Add button.

image from book

The design screen like the following should appear. When it does, type in the program as you see in the graphic below:

image from book

Notice that some of the words are in blue. This should help you notice with a word is spellled correctly.

Once the program has been written, select the Build option on the menu bar. A popup menu like the following should appear. Click on the Build Solution option.

image from book

Selecting the Build Solution option compiles (compiling means transfers the program to machine language) and links the program (linking means adds extra code that was specified by the program and the IDE). That is the program will attach any additionally required files and then convert the language into a machine language .exe file.

 Note:  Since this program is being compiled in Visual Studio .NET, the language being used is not the C++ that you may use with a different compiler. Other compilers may use either Classical C++ or Standard C++. The version of C++ that was used prior to 1997 standardization is today called Classical C++. The version of C++ that resulted from the 1997 standardization is referred to as Standard C++

The process of using the Build in Visual Studio .NET is to convert the source code that you see above, into machine language which is an .exe file. If the program is in error, error messages will appear below the source code. There are two major types of errors: syntax errors and warning errors. The program will not run if there are syntax errors so they must be fixed. The program will run if there are warning errors but it may not run correctly. There may be something seriously wrong with the code when there are warning errors. Your programs must never have either types of errors. Notice at the bottom of the screen when you compile the program and there are no errors should look like the following:

image from book

Now that the program is compiled, it is time to run it. This is done by clicking on the Debug option on the Menu bar. (Notice that this can also be accomplished by holding down the two keys: Ctrl+F5.) When you do this, a popup menu like the following should appear:

image from book

Click the Start Without Debugging option. When you do, a console screen like the following should appear showing the output to the screen.

image from book

This completes the demonstration of how to use the Visual C++ compiler in Visual Studio .NET.

 Note:  Using Windows Explorer look at the files created by this process. Link to the file folder in which you saved the program. First look at the folder: test You should see the following files:

  • test.cpp

  • test.ncb

  • test.sln

  • test.suo

Notice that there is a sub folder called: Debug. Look into that folder and note that there are the following files:

  • text.exe

  • test.ilk

  • test.pdb

In addition to the Debug sub folder, there is a folder by the name of test (This name would be different for a different program.) Open the test folder and you will see the following files:

  • test.cpp

  • test.vcproj

  • test.vcproj.S44N124.dvoils.user (in place of my name should be yours)

In the test folder there is a Debug folder. In that folder are the following files:

  • BuildLog.htm

  • mt.dep

  • test.exe.embed.manifest

  • test.exe.embed.manifest.res

  • test.exe.intermediate.manifest

  • test.obj

  • vc80.idb

  • vc80.pcb

The file text.cpp is the C++ source code and test.exe is the compiled program. If you wanted to transfer your program to a client, you would send them the program test.exe

To practice this process, close this project and create the program test2.exe . For this process, follow the same steps as you saw above. For the source file: test2.cpp, type in the following (for now do not concern yourself with what the words mean)

image from book

 #include<iostream> #include<string> using namespace std; int main() {    string name;    cout << endl << "3 + 4 = " << 3+4 << " and that is the truth";    cout << endl << "What is your name? ";    cin >> name;    cout << endl << "Did you say your name was " << name << "?" << endl << endl;    return 0; } 

image from book

 Note:  Instead of retyping the code from the notes into the compiler, it is more time efficient to just copy. This can be done by blocking the example and then clicking ctrl-C (which stands for copy blocked text). Once you are inside of the compiler where the code is to appear, click ctrl-V (which means paste.) In the notes, when the sample code is captured in a file that contains only the code, you should first click ctrl-A (which means block the page) before you click ctrl-C.

 Note:  If an error occurs during compiling, a message will appear below the code in the Output box. If you double click any error message, the IDE will transfer your cursor to a spot either where the error is located or just below the error.

 Note:  When you create a C++ program file, it has the extension of .cpp. When you submit the homework, I want you to change to have the extension of .txt. To do this, click on the code, click ctrl-A followed by clicking ctrl-C . Next open Notepad. Inside of Notepad click ctrl-V. This will copy all of the code from Visual Studio into Notepad. From inside of Notepad you can save it. Notepad provides the .txt extension.

Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
Year: 2007
Pages: 142 © 2008-2017.
If you may any questions please contact us: