C++
How to enable C17 compiling in Visual Studio
Embracing modern C++ standards is crucial for developers aiming to write efficient, robust, and maintainable code. C++17, ratified in 2017, brought a wealth of new features and improvements, making code more concise and expressive. If you’re working with Visual Studio, understanding how to enable C++17 compiling in Visual Studio is a fundamental step to leverage these advancements. This guide will walk you through the process, ensuring your projects can harness the power of structured bindings, std::optional, std::variant, and other essential additions. By configuring your environment correctly, you unlock a more streamlined development experience and access to cutting-edge language constructs that enhance productivity and code quality. Let’s delve into the specifics of setting up your Visual Studio environment for C++17.
Understanding C++17 and Visual Studio’s Role
C++17, officially known as ISO/IEC 14882:2017, is a significant iteration of the C++ programming language standard. It introduced several game-changing features designed to simplify common programming tasks and improve overall code clarity. Key additions include structured bindings, which allow for elegantly unpacking elements from tuples or structs; std::optional, providing a clear way to represent optional values; and std::variant, offering a type-safe union. These features, among others, contribute to writing more expressive, safer, and often more performant code, moving away from older, error-prone patterns.
Visual Studio, Microsoft’s integrated development environment (IDE), plays a pivotal role for C++ developers on Windows. Its C++ compiler, MSVC (Microsoft Visual C++), strives for excellent conformance with the ISO C++ standard. While older versions of Visual Studio might require specific updates or configurations to fully support C++17, modern versions like Visual Studio 2019 and 2022 offer robust support out-of-the-box. The continuous evolution of MSVC ensures that developers can adopt the latest C++ standards, including C++17 and beyond, without significant hurdles. For instance, Visual Studio 2017 version 15.3 and later provided initial C++17 feature completeness, with subsequent updates refining conformance and fixing bugs.
According to Microsoft’s documentation on C++ language conformance, MSVC aims for the highest level of standard adherence. This commitment means that once you configure your project to use the C++17 language standard, you can expect reliable compilation of C++17-specific syntax and library features. Leveraging these features not only modernizes your codebase but also makes it easier to collaborate with other developers who are accustomed to contemporary C++ practices. The integration within Visual Studio makes the process of enabling C++17 compiling straightforward, allowing you to focus on your code rather than complex build configurations.
Prerequisites: Visual Studio Installation and Updates
Before you can successfully enable C++17 compiling in Visual Studio, it’s essential to ensure your development environment meets certain requirements. The primary prerequisite is a sufficiently recent version of Visual Studio. While Visual Studio 2017 (specifically version 15.3 or later) was the first to offer C++17 feature completeness, it’s highly recommended to use Visual Studio 2019 or, preferably, Visual Studio 2022. These newer versions come with more up-to-date MSVC toolsets, offering superior C++ standard conformance, better performance optimizations, and fewer compiler bugs. An outdated Visual Studio version might either lack full C++17 support or require additional manual updates that are less straightforward.
Once you have a suitable Visual Studio version installed, you must verify that the “Desktop development with C++” workload is part of your installation. This workload includes the necessary MSVC compiler, libraries, and build tools required for C++ development. If you omitted this workload during the initial installation, you can easily add it by launching the Visual Studio Installer, selecting your installed Visual Studio edition, and clicking “Modify.” Within the “Workloads” tab, ensure “Desktop development with C++” is checked. It’s also crucial to keep your MSVC toolset updated. The toolset contains the C++ compiler, linker, and other components. Regular updates via the Visual Studio Installer will provide you with the latest compiler features and bug fixes, ensuring optimal C++17 support and enhanced code quality.
Keeping your Visual Studio installation current is not just about accessing new C++ features; it’s also about security and performance. Microsoft frequently releases updates that patch vulnerabilities and improve the overall stability and speed of the IDE and its compilers. A well-maintained installation with the latest MSVC toolset provides the best foundation for enabling and utilizing C++17 efficiently. Without these fundamental components, attempts to compile C++17 code will likely result in compilation errors or incomplete feature support. Therefore, taking a few moments to check and update your Visual Studio environment is a critical first step.
To enable C++17 compiling in Visual Studio for your specific project, you need to modify its project properties. This process is straightforward and ensures that the MSVC compiler uses the correct language standard when building your code. The key is to navigate to the C/C++ settings and select the appropriate language standard. This action applies a specific compiler flag (e.g., /std:c++17) to your project’s build configuration, instructing the compiler to recognize and correctly process C++17 syntax and library features.
Here are the detailed steps to configure your Visual Studio project for C++17:
-
Open Your Project or Solution: Launch Visual Studio and open the C++ project for which you want to enable C++17.
-
Access Project Properties: In the Solution Explorer, right-click on your project (not the solution) and select “Properties” from the context menu. This will open the project’s Property Pages dialog.
-
Navigate to C/C++ > Language: In the Property Pages dialog, on the left-hand pane, expand “Configuration Properties,” then “C/C++,” and select “Language.”
-
**Set the Question & Answer :
I want to use C++17 features.How can I switch compiling from C++14 to C++17 in Microsoft Visual Studio?
Or is it not available in release versions of VS?
There’s now a drop down (at least since VS 2017.3.5) where you can specifically select C++17. The available options are (under project > Properties > C/C++ > Language > C++ Language Standard)
- ISO C++14 Standard. msvc command line option:
/std:c++14 - ISO C++17 Standard. msvc command line option:
/std:c++17
Visual Studio 2022 (MSVC C++20 and the /std:c++20 Switch - C++ Team Blog):
- ISO C++20 Standard. msvc command line option:
/std:c++20
Any Visual Studio:
- The latest draft standard. msvc command line option:
/std:c++latest**
- ISO C++14 Standard. msvc command line option: