Introduction
To create a project in C or C++, you can use many IDE's (Integrated Development Environment) and Compilers in Windows to choose from. The one we are using in this tutorial will be Visual Studio Express 2010(Using Visual C++). What we will go through is the basic way of creating a simple executable program. We will go through this slowly so you can understand the main workings to get you started.
Installing Visual Studio 2010 Express
Firstly we have to obtain the latest Visual Studio Express, which is available for free here at Microsoft's Website. You can get the more advanced versions of Visual Studio 2010 (Standard, Professional, and others), but only if you have the finances as it has more advanced features for more advanced programming. Express still fits its purpose as being a solid and powerful IDE to create powerful programs. First on the download page, you have two options. You can select two different ways of installing. You can simply have a web install, which downloads only Visual C++ (The only one you will need), or you can download the ISO disc image, which has all the Express product suite, and you can pick and choose which one you want to install. I always feel better using the ISO, so if things do not install properly, at least I can install via the disc off-line without running through the web install every time i want to add any of the programs in the suite. So we will download the image, which should be around 700MB in size.
Once you have downloaded the ISO image, we need to burn it. You can burn it on any commercial CD burning program, but I admit, for the budget conscious I would recommend CDBurnerXP, as it is free to use and burns CD/DVD's as good as I've seen from any program of its kind. So once you are ready, burn the ISO image on a CD or DVD and away we go. Installing Visual Studio is ready to start, so install the disc so the auto start prompt comes up (See below). Select the third option (as we are downloading Visual C++ 2010). Follow the instructions and Visual C++ will be ready to use.
Starting Your First New Project
Now we have started, we have to open the program and set up a project for us to work on (Should be in your Programs category in your Start Menu). Firstly on the main home start page which displays when you open Visual C++, you will notice that there are two options in the top left corner of the screen.
Select New Project. Once you have done that, a new prompt window will appear (See Below).
In the middle menu, select Win32 Console Application, as this is option which we will start with. The program will appear in a console window, as it output simple text and nothing else for now. Other options we will go through over later tutorials. Now we will give this project a name, this is important as this will be the name of the complete executable (.EXE). So next to Name, enter in the field the name of your choice. In this instance it will be 'HelloWorld'. In the field below, we have the location of where all the project files will be stored and saved. You can locate your project anywhere on your hard drive. Keep it inside your own Documents directory for simplicity. Make sure you remember where it is. Below that field, you have the Solution name. As you have noticed, it mimics the name of your project; if you want to give it a different name, it is possible. The Solution in which we are referring to can be described as the manager of the project/s, as it generates a .sln file, which manages the project files (.vcxproj & .filters) and other important key elements which Visual C++ handles automatically (Intellisense). Project files refers to all the source files and libraries you may need in a project. Each project file manages the build of a executable program and output (Generates an executable inside Debug or Release folder, more on that in later tutorials). It is important to know that you can include as many project files into a Solution that you wish; this will come in handy for when you are looking at developing extra programs, libraries and extensions for your executable program.
Understanding Projects and File Structure
If you want to create a folder, which has the same name as the Solution (which will be placed inside the folder you have chosen in the location field and you can also save the solution with project and source files inside), make sure that the check box is checked.
Once you have clicked Yes, the Application wizard will appear (See Above). On the left hand side, click on Application Settings so you will have the view shown above. Make sure that the Application type radio button is clicked on Console Application, and on 'Additional Options' make sure the 'Empty Project' check box is checked, so we do not end up with useless source code we do not need. With that done, press Finish then the Solution workspace is ready to work on. As you can see, there is a dockable window on the left of the screen. That is called a Solution Explorer, and that is where all the project and source files are handled within the main Solution. The Project file we are working with is already there, but there is no source files. You can either add a pre-existing source file or create a new one.
Creating and Using Source Code Files to Work With
You can add a pre-existing one, which would be best stored within the Solution directory place. You can select the file that you may need by going into the Solution Explorer, and then right click on the project folder, and then on the menu select Add > Existing Item (easier way is to press Shift-Alt-A. If it is a C (source) file, it will filter into the 'Source Files' folder in the window (it will not physically move on your directory drive), or if it is a H (header) file the filter will send it inside 'Header Files' (Hint of Irony and logic!). This is something for you to remember during going through the tutorials later on. Now in our example, instead of using a file that already exists (as you know there isn't one to work with!), we will create a source file which will be called main.c.
First we can do this by three different ways; either Select File > New > File, Right click on Solution Explorer, (See above) and select Add > New Item (shown above), or just simply press Ctrl-Shift-A. Name the file appropriately (See Below), so at least we have a managed source file to work with.
With the file ready for coding, it is important to follow the strict rules in the syntax to use. Or you code will not compile. There's no stopping now, if you use computing logic.
Ready to Compile and Build the Source Code
Now we are ready to work on the code, it is important to know what to do when you have finished coding and ready to build the program. In the Output Window, (See Below) at the default bottom position of the Visual Studio, you can see if the program has any syntax errors, by building your Solution via the compiler. That can be achieved by either Build > Build Solution or just pressing the F7 key. If your code has errors, the Output window will notify you with error messages (listing the places where the errors are in the source code), which means that you will not be allowed to build and compile your program, unless you fix your errors in the code source files.
Running the Application
If the code is successfully built without any errors, you will can easily run the program by either pressing the F5 key, or going into the main menu and select Debug > Start Debugging. Now the program, as in our example will run (See Below).
The project we have just created will be available as an tutorial in the Hello World article inside the Starting C Programming tutorials section. Where we will go through the code in more depth.
Configuring your Project
With that, the real beauty of Visual C++ is that it is so easy to link your header files or static libraries, which will be more important for more advanced programs than the 'Hello World' we have demonstrated. In a solution and project, you have to point where you want to keep your header files, or libraries. Thankfully this is easy, as this can be done going to Project Properties, which is found in Project > Properties in the main menu. There are many configurations you can deal with, but in this instance we will refer only to headers and libraries.
Using Header Files
First of all, we are going to link a directory folder, which has header files. This is helpful, especially if you going to be using an external library (static libraries need header files to work in Visual C++). In that case you do not want store the header locally in the solution source code folder. If you use your own header file, which is local, in your code you call it like this.
#include "foo.h"
As you can see, the header file is called inside quotation marks, as this links to the relative directory path. You can link to any local folders using the normal directory conventions (Such as sub-folder paths, parent directories, forward slashes, etc.). If this file isn't in the place specifically located, then you will generate major errors in the compiler.
Many developers store headers and libraries in different areas on their hard drives to where there projects are stored, not only to separate files, which can risk your project files becoming more complicated to manage than what it should be; the files can be used for a multitude of different projects. So we will have to add the include path in Visual C++, which will lookup when you call the following header file. As angles brackets denote that the header file is in a include path already pre-defined in Visual C++ and ready to use with the compiler. If not, it will again return errors on build.
#include <foo.h>
To do that, Go into the Project Properties dialogue window (See below). Once you have it open go into Configuration Properties > C/C++ > General on the left hand menu panel. Then on the right, with the selection of options. Go to Additional Include Directories, and from there you can select the directories, that you will need.
Understanding Library Files
It is important if you have libraries to link them to your project. You can either keep the library (or .lib) files local and call on top of your source/header file just below the include directives (See below).
#pragma comment(lib,"foo.lib")
When you have a few libraries, it will encounter the same problem you will have that can happen when too many headers are allowed to reside in your relative project directory. You can refer to the libraries and their folders through Visual C++. First open up Project Properties again, then select Configuration Properties > Linker > General on the left hand menu (shown below).
On the right, you can select the directories where your library files (.lib) are located by selecting Additional Library Directories and picking the folders. The path will be referenced inside the compiler through its lookup path feature. You can use the library file either by the #pragma comment directive like shown above, or by going through the Project Properties section and going into Configuration Properties > Linker > Input on the left menu. On the right, Select Additional Dependencies, and you can enter the name of each library file foo.lib, followed by a semi-colon separator. Alternatively, you can use the Command Line to link the lib files. This can be located in the Project Properties at the left menu; Configuration Properties > Linker > Command Line.
Summary
When you are developing larger programs with Visual Studio, you may have no choice other than using these commands and configurations. Once you get used to it, things will be more straightforward to set up. With most C/C++ tutorials on this site, there are Visual Studio Solutions for you to download and learn from with the reading material.