Programming in C++ with Visual Studio.Net 1) Using the Command-Line (unmanaged code)

advertisement
Programming in C++ with Visual Studio.Net (unmanaged code)
1) Using the Command-Line
Let’s compile a simple program like hello.cpp (shown below) from the command line.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n" ;
return 0;
}
The first thing to do is to start up a Command Prompt window. As we will need to set up appropriate paths to the
compilers and other tools, the best bet is to use the Command Prompt provided by Visual Studio.Net itself. Select
the menu options:
Start -> 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 Studio.Net IDE
To fire up Visual Studio.Net, use the menu options:
Start -> Programs -> Microsoft Visual Studio .NET -> Microsoft Visual Studio .NET
This will start up VS.Net, and present you with the start page:
Either click on the New Project button, or select the menu options:
File -> New -> Project
VS.Net then displays the New Project dialog box:
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:
Click on the Application Settings option to bring up another dialog:
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.)
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
(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.)
This will bring up the Add New Item dialog window:
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:
To compile, link and execute this code, you can either click on the Start Button
, press F5, or use the
Debug -> Start
menu options. VC++ will first ask permission to compile your code:
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.
Run again to get the expected output:
An alternative is to use Ctrl-F5 (Start without debugging), which automatically hangs the DOS window for you:
Again, just hit enter to shut down the DOS window.
Download