Utilization of Visual Studio
Creating a new project
1. From the home page of Visual Studio, click on Create a new project.
2. Double-click on Console App.
3. Enter the name of the project in the first text box (From the top).
4. Choose the location inside the file system where to save the project from the button with the button with the 3 dots. Then click on the button Create.
5. The project is now created.
Creating a new file
The files of the project are shown at the right of the window. By default, the file “main.cpp” is created, containing the code of a simple program.
There are two types of code files in C++: Source files (With the extension .cpp) and header files (With either the extension .hpp or .h).
Source files should be added in the “Source Files” folder and header files in the “Header Files” folder.
1. Right-click on the folder (At the right of the window) you want to add the file to. Then go on Add and click on New item....
2. Select the type of the file to create (Source of header), enter the name of the file and then click on Add.
3. The file is now created and added to the project.
There is, at the left of the window, the code editing zone. By double-clicking in the name of a file, from the file list at the right, we can open it inside the code editing zone.
Here is the content of the root folder of the project (Before any compilation):
The file with the extension “.sln” is the file representing the project. Double-clicking on it will open the project in Visual Studio. It however does not contain the code files of the project. They are in the folder named the same as the project.
We can see the header file FileName.h and the source file ProjectName.cpp.
Compiling the project
We can compile and run the project (In debug mode) by clicking on the button Local Windows Debugger.
We can choose to compile the project in 32 bits (x86) or 64 bits (x64) with the selection box before the button to compile and run the project.
We can choose between compiling the project in debug or release mode with the selection box before the selection box to choose between compiling in 64 or 32 bits.
By clicking on the menu item Build and then Build ProjectName, we can build the project without running it.
Sometimes, when some code files has not changed since the last compilation, they are not recompiled. It is possible to recompile the whole project by selecting the menu item Build and then clicking on Rebuild Solution.
We can run the executable of the project in release (non-debug) mode by sectioning the menu item Debug and the clicking on Start Without Debugging.
Running the program in debug or normal mode is very similar. The only real difference is that when we run it in debug mode, we have access to additional tools to help us find bugs. We will learn how to use those tools later.
Here is the console window appearing when we compile and run the program:
What the default code written in the file FileName.cpp does is print the text "Hello World!" to the console and then stop.
The executable file produced
Now that we compiled the project, a new folder, containing the executable, as been created inside the root folder of the project.
Here is the executable:
This is the executable file produced by the compilation of the project. We could run it by double-clicking on it, but we would see nothing. Why? The reason is that when it is not executed from the IDE, the program closes right after its execution. Since it is executed extremely fast, we do not have the time to see what appears inside the console.
If we set the compilation mode to release:
And compile and run it:
The result is the same:
But the executable file has been compiled in release mode:
As we can see, the executable file, in release mode, weights 11 KB, while the same executable file, compiled in debug mode, weights 47KB.