Windows Setup (Visual Studio)

System setup (Visual Studio)

General Information on Visual Studio

The default approach to compile C++ code in Windows is the use of Microsoft Visual Studio.
Visual Studio is large software that takes time and space to be installed. Make sure you prepare this installation in advance while having access to
Beware: Do not confuse "Visual Studio" with "Visual Studio Code" they are two different softwares.

Installation steps

These steps need to be done only once on a given computer.
Before starting, make sure your Windows system is updated (ex. on Windows 10: Settings, Update & Security, Windows Update). If your system is not updated since months/years, Visual Studio may not be able to correctly compile C++14 which is required for CGP.

As a general advice, setting the primary language in your Windows settings to English (instead of French for instance) is a good idea when programming. Several settings and debug message will make much more sense in English than their (sometimes approximated) translations in other language.

1. CMake

First download and install CMake
assets/cmake_install_find.jpg
assets/cmake_install_addpath.jpg
CMake is a cross-plateform software that can convert a generic project description provided in the CMakeLists.txt file into a specific Visual Studio project on Windows.

2. Visual Studio

Warning: Make sure the installation process of CMake is fully finished before starting the installation of Visual Studio.
Second, download and install Microsoft Visual Studio.
During the installation process, make sure you select the Desktop development with C++ otherwise you will not get the C++ compiler (the default options that comes with this options are good).
assets/visual_install.jpg
Notes:

First compilation

The general process to compile and execute a C++ project with Visual Studio will be the following.
Preliminary notes:
  • \(\Rightarrow\) If you are using Avast: You need to desactivate it otherwise it will block the creation of new executable files.
    • Side Note: The default "Windows Defender" (on Windows 10 and 11) which is automatically activated when you don't have other antivirus is already a good protection for standard usage against virus. Avast is probably not the best tool to use in general for protecting your computer.
  • \(\Rightarrow\) The use of "Dropbox" (or file synchronization type of tools) should also not be activated on the directories where you generate your project.

Method 1.a Generating a Visual Studio project from the provided scripts

Let us consider the root_directory to be examples/example_compilation/
Go to the directory [root_directory]/scripts/ (ex. examples/example_compilation/scripts/), and double click on the file windows_cmake_visual.bat.
assets/script_cmake_visual.jpg
Rem. This script assume that CMake is visible from the command line path, and that Visual Studio is the default compiler.

Method 1.b Generating a Visual Studio project from CMake manually

1. Start CMake as an external tool - Call cmake-gui
assets/call_cmake.jpg
Calling CMake as an external program
2. Indicate the path of the project you want to compile in the CMake interface.
assets/cmake_path.jpg
Set the path to your root directory
Reminder: Avoids in general "non standard" path (accent letters and/or space) to place your code when using CMake.
3. Run CMake
assets/cmake_build.jpg
Accept the creation of the build/ directory.
assets/cmake_configure.jpg
Set your version of Visual Studio.
assets/cmake_configure_finished.jpg
End of the configuration indicating "Configuring done".
assets/cmake_files.jpg
Note: Windows may display the files but hide their extensions (.sln) in its default configuration. In this case you can explicitely request to display them in the "View" planel of the file explorer (for Windows 10 and later).
assets/windows_file_extension.jpg

Opening and running the project in Visual Studio

The project can now be opened in Visual Studio either
Depending on your system, Visual Studio may display a more or less empty screen.

Opening the source code of the project in starting Visual Studio
Note: If the "Solution Explorer" frame does not appear, you can explicitly ask for it in "View -> Solution Explorer".
\(\Rightarrow\) Your Visual Studio should look like this at the end.
assets/visual_studio_screen_final.jpg
Opening the source code of the project in starting Visual Studio
\(\Rightarrow\) The compilation of your program can be started using the shortkey F7
\(\Rightarrow\) The code can be run in clicking on the green arrow Local Windows Debugger.

Additional information on build/

The build directory can take a lot of space on your disk, but it contains only temporary files for the compilation process that depends on your machine (depending on your absolute file paths).
This build/ directory can (and should) be suppressed without risk when you are not editing the project.
In particular:

Method 2: Direct load in Visual Studio

Another possibility is to use the internal CMake tool of Visual Studio.
1. Open Visual Studio, and select Open a local folder
2. Go to the root directory of your project, and click on Select Folder \(\Rightarrow\) the project will then open in Visual Studio
- The shortkey F7 (or CTRL+F7) start the compilation of the project.
- Once the compilation is finished, right click on example_compilation.exe in the Solution Explorer, and select: Set as Startup Item. - The program can then be run using the green arrow "Current Document" when looking at a source file of the project. Or in setting it permanently this to the executable name (ex. example_compilation.exe) using the down arrow in the right-side of the button.

Opening the source code of the project using the embedded CMake in Visual Studio
Note: