Would you like to compile a C++ program on Visual Studio? Let’s do it.
In this tutorial, we’ll be using Visual Studio as our IDE (not Visual Studio Code). Before we get started, you’ll need to have:
*When installing Visual Studio, ensure that you select “Desktop development with C++” under the workloads tab. Now that we’re all set, let’s get started.
Step 1: Compile a New Project
Start off by opening Visual Studio on Windows and select “Create a new project”. After this, you’ll be able to choose a template. Select an Empty project and give your project a name. For example: VS_example.
Step 2: Load a C++ File
In the main Visual Studio window, select file and open the C++ file you’d like to load onto the project. The code in your file will be loaded on the left side of the screen. The file, however, hasn’t been loaded as a source file. To do this, open file explorer on your PC then click and drag your C++ file to the Source Files folder. This is in the Solution Explorer Window on the right. You’ll now see the file under Source Files.
Step 3: Compile Source Files
Let’s now try compiling our code files. Make sure that your settings at the top of Visual Studio read “Debug” and x64 (for Windows 64) or x86 (for Windows 32). Then right click the project name to select “Build”. This will result in an error message in the Error List window.
Step 4: Identify the Error
In the Error List, the error message states that there is a command in the C++ code, called include. The file, octeract.h, that this command, is calling cannot be found.
Step 5: Include the Header
To show Visual Studio where to find this file, let’s go to properties by right clicking on the project folder. Ensure that at the top of the properties window configuration is set to “Debug” and the Platform is set to x64 or Win32 (for Windows 64 or Windows 32 respectively). Select C/C++ and choose to edit Additional Include Directories. This will open another window. In the empty space, paste the path to the folder which has the header file. For example: C:\Program Files (x86)\Octeract\include. Then press OK and apply this change.
Step 6: Rebuild
From here, you can try to compile the source files again by right clicking on the project name and selecting rebuild. This will result in some different errors. These errors show there are commands used in our code that are defined in a library which the compiler cannot find. You’ll need to link to this library.
Step 7: Link to the Library
To show the compiler where to find the library, right click on the project folder and select properties. In this window, ensure that the configuration is set to “Debug” and the Platform to x64 (for Windows 64) or Win32 (for Windows 32). Select “Linker” and use the drop-down arrow to choose “Input” from the list of Linker properties. Select Additional Dependencies to edit. This will open another window. In the empty space, paste the path to the library you want to link to. For example: C:\Program Files (x86)\Octeract\bin\libocteract.lib. Then press OK and apply the change.
Step 8: Compile the Program
From here, right click on the project name and choose “Rebuild”. There should now be no errors. No bugs means that we can start compiling. To do this, change the setting at the top of Visual Studio from “Debug” to “Release”. Right click on the project folder to open Properties. In this window, change the configuration to “Release”. We’ll then include the header and link to the library. To do this, follow steps 5 and 7 above. Once the paths have been added, right click on the project name and select rebuild. The C++ program is now compiled and the compiler has created an .exe file.
Step 9: Execute the File (Optional)
You’ll now be able to execute this file from a PowerShell session. To do this, simply use the path where the .exe file is saved. For example: C:\Users\Octeract\source\repos\VS_example\x64\Release\VS_example.exe.
From here, you’ll be able to see the solution printed on the screen in PowerShell.