Visual Studio Quick Start
Generally, code organized by Visual Studio appears as a Solution, which contains a collection of Projects. When a codebase is organized this way, it includes a .sln file (which configures the solution) as well as .vcxproj files (which configure each project); these files help define things like include paths, compiler settings, and how the projects are connected.
Visual Studio also supports an Open Folder mode as of Visual Studio 2017 which does away with .sln and .vcxproj files and allows you as the user to configure your own environment independently. This approach is ideal for cross-platform projects that will be run from a variety of different IDEs or editors. Better yet, if you are a CMake user, as of Visual Studio 2017 there is a built-in CMake experience. This guide will not go over Open Folder or CMake, but you are encouraged to check out the relevant blog posts linked in this paragraph for more information.
One of the most useful features for helping you write code quickly in Visual Studio is *IntelliSense*, which is a context-aware code completion tool. As you type, Visual Studio will suggest classes, methods, objects, code snippets, and more symbols that make sense in relation to what you have typed so far and where you have typed it. You can scroll up and down the suggestions with the *arrow keys*, and complete symbols with *Tab*.
To efficiently write and understand code, easy code navigation is essential. By using the *Go To* feature *(Ctrl+T)* you can quickly get to where you need to go, without taking your hands off the keyboard. When you open the dialog, you can filter your results by clicking on the desired button, or by starting your query with a specific token. For example, you can go to a specific file by opening the *Go To* dialog and typing “*f* ”. Another common way to access this dialog is by going to a specific line number; you can do this by opening the menu traditionally and using the “:” token, or by pressing *Ctrl+G*. Try using *Go To* to navigate around the demo project.
Peek/Go to Definition
Sometimes it can be challenging to find out where a function or object is defined in your codebase. This problem is easily solved in Visual Studio, where you can easily peek into definitions. Try this in the demo project by selecting the function you want to look at, and pressing Alt+F12, or selecting it from the right-click menu. This will bring up a preview of the file where the function is defined, where you can quickly make small edits. Press Esc to close the preview window. You can also go directly to the definition by pressing only F12.
You can also use Visual Studio to refactor existing code. In the demo project, there is a function that has an unhelpful name. Rather than going to each file to change the name of each occurrence manually, choose one of the functions and press Ctrl+R, Ctrl+R or right-click it and choose Rename. This will bring up a menu where you can choose what you want to rename it to, and then preview the changes before they are committed.
Debugging and Diagnosing Issues
Once you can successfully build your application and write code easily, the next step is often debugging the application. Debugging can be a complex process, and Visual Studio provides many powerful tools to help along the way. The most commonly used debugging tool is the breakpoint, so let’s start with that. If you click on the bar to the left of your code, a red circle should appear. If you click the circle, the breakpoint will be removed.
When a breakpoint is set and the program reaches that point of execution, it will stop, allowing you to inspect variables and the current state of the program.
To begin debugging, you can either press the green arrow at the top of the IDE or press F5. Once the program has stopped on the breakpoint, there are many things you can do to help you diagnose problems. One of the best ways to find problems is to understand the current state of the program, versus what it should be. This can be easily achieved by using the Autos Window, which lists recently used variables and their values. You can also hover your mouse over a variable to see what the current value is. When you have sufficiently understood the current state of the program, you can press the green arrow button or press F5 again to have the program run until the next breakpoint.
Check out the C++ Debugging and Diagnostics blog post to learn more.
Visual Studio has a built-in test framework to help you unit test your projects, ensuring that the code you write is working as expected. To test the demo project, which is a native console application, you can add a Native Unit Test Project to the solution.
Add a test project to the demo. This is done by going to File > New > Project then selecting Visual C++ > Test > Native Unit Test Project. Make sure to choose the Add to solution option in the Solution dropdown. You can also simply right-click your solution name in the Solution Explorer and choose Add > New Project to accomplish the same task.
Once you have added a unit test, you can open the .cpp file and see the basic testing skeleton in the template, and begin to add tests.
Once you have added a test, you can run the test by going to Test > Run > All Tests in the menu at the top of the IDE. Once you have run the tests, you will see the results in the Test Explorer window.
Check out the C++ Unit Testing in Visual Studio blog post to learn more.
Working with A Team
It is very common these days to be working on a project with a team, and Visual Studio makes collaboration with others easy! You can easily create new source control repositories using Git or Team Foundation Server to manage your codebase. To create a new repo for a project, click the Add to Source Control button at the bottom of the screen, and add the opened project to the source control system of your choice.
Once you do that, a local repository will be made for your project. From here you can make commits, or push your changes to a remote Gitservice such as GitHub. This is all managed in the Team Explorer window.
You can also very easily clone from source control from the Team Explorer window. Choose Projects > New Repository, and then follow the prompts to clone the project. From here, all you must do is paste in the URL, and the project will be cloned.
To learn more about working on a project as a team in Visual Studio, check out the Visual Studio for Teams of C++ Developers blog post.
Visual Studio by default catches a lot of code issues, but its Code Analysis tool can often uncover hard-to-find issues that would normally be missed. Common errors that are reported include buffer overflows, uninitialized memory, null pointer dereferences, and memory and resource leaks. This functionality is built into the IDE, and can easily be used to help you write better code. Try it out by going to the Analyze menu and choosing Run Code Analysis > On Solution. Learn more about Code Analysis as well as the C++ Core Guidelines Checkers in the announcement blog post.
Library acquisition in C++ can be challenging. While Visual Studio has support for NuGet package management, more recently a new tool called vcpkg was launched. Vcpkg is an open source tool maintained by Microsoft that simplifies acquiring and building open source libraries, with over 200 currently supported. This tool, while separate from Visual Studio itself, is a valuable companion for any C++ developer on Windows. Check out the announcement blog post for details.