Keywords: Visual Studio | compiler version detection | C++
Abstract: This article provides an in-depth exploration of how to detect the Microsoft Visual Studio compiler version in C++ development. By analyzing the usage of predefined macros _MSC_VER and _MSC_FULL_VER, it offers a complete version mapping table from Visual Studio 97 to Visual Studio 2022. The article also discusses best practices for version detection, including handling version ranges and avoiding common pitfalls, providing practical guidance for cross-platform compatibility and conditional compilation.
Introduction
In C++ development, particularly in cross-platform or backward-compatibility scenarios, detecting the current compiler version is a common requirement. For Microsoft Visual Studio users, accurately identifying the compiler version not only aids in conditional compilation but also ensures code consistency across different development environments. This article delves into how to detect Visual Studio compiler versions through predefined macros, providing detailed technical implementations and best practices.
Core Macros: _MSC_VER and _MSC_FULL_VER
The Microsoft Visual C++ compiler defines two key macros for version detection: _MSC_VER and _MSC_FULL_VER. These macros are automatically defined during compilation and can be directly referenced in code.
The _MSC_VER macro represents the compiler's major version number, typically presented as an integer. For example, Visual Studio 2019 corresponds to a _MSC_VER value of 1920. This macro is the most commonly used tool for version detection as it provides sufficient information to distinguish major Visual Studio versions.
The _MSC_FULL_VER macro offers more detailed version information, including patch levels and build numbers. For instance, Visual Studio 2008 SP1 has a _MSC_FULL_VER value of 150030729. This macro is particularly useful in scenarios requiring precise version matching.
Version Mapping Table
The following is a complete version mapping table from Visual Studio 97 to Visual Studio 2022, showing the relationship between _MSC_VER values and corresponding Visual Studio versions:
MSVC++ 14.30 _MSC_VER == 1933 (Visual Studio 2022 version 17.3.4)
MSVC++ 14.30 _MSC_VER == 1932 (Visual Studio 2022 version 17.2.2)
MSVC++ 14.30 _MSC_VER == 1930 (Visual Studio 2022 version 17.0.2)
MSVC++ 14.30 _MSC_VER == 1930 (Visual Studio 2022 version 17.0.1)
MSVC++ 14.28 _MSC_VER == 1929 (Visual Studio 2019 version 16.11.2)
MSVC++ 14.28 _MSC_VER == 1928 (Visual Studio 2019 version 16.9.2)
MSVC++ 14.28 _MSC_VER == 1928 (Visual Studio 2019 version 16.8.2)
MSVC++ 14.28 _MSC_VER == 1928 (Visual Studio 2019 version 16.8.1)
MSVC++ 14.27 _MSC_VER == 1927 (Visual Studio 2019 version 16.7)
MSVC++ 14.26 _MSC_VER == 1926 (Visual Studio 2019 version 16.6.2)
MSVC++ 14.25 _MSC_VER == 1925 (Visual Studio 2019 version 16.5.1)
MSVC++ 14.24 _MSC_VER == 1924 (Visual Studio 2019 version 16.4)
MSVC++ 14.23 _MSC_VER == 1923 (Visual Studio 2019 version 16.3)
MSVC++ 14.22 _MSC_VER == 1922 (Visual Studio 2019 version 16.2)
MSVC++ 14.21 _MSC_VER == 1921 (Visual Studio 2019 version 16.1)
MSVC++ 14.2 _MSC_VER == 1920 (Visual Studio 2019 version 16.0)
MSVC++ 14.16 _MSC_VER == 1916 (Visual Studio 2017 version 15.9)
MSVC++ 14.15 _MSC_VER == 1915 (Visual Studio 2017 version 15.8)
MSVC++ 14.14 _MSC_VER == 1914 (Visual Studio 2017 version 15.7)
MSVC++ 14.13 _MSC_VER == 1913 (Visual Studio 2017 version 15.6)
MSVC++ 14.12 _MSC_VER == 1912 (Visual Studio 2017 version 15.5)
MSVC++ 14.11 _MSC_VER == 1911 (Visual Studio 2017 version 15.3)
MSVC++ 14.1 _MSC_VER == 1910 (Visual Studio 2017 version 15.0)
MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015 version 14.0)
MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013 version 12.0)
MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012 version 11.0)
MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010 version 10.0)
MSVC++ 9.0 _MSC_FULL_VER == 150030729 (Visual Studio 2008, SP1)
MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008 version 9.0)
MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005 version 8.0)
MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio .NET 2003 version 7.1)
MSVC++ 7.0 _MSC_VER == 1300 (Visual Studio .NET 2002 version 7.0)
MSVC++ 6.0 _MSC_VER == 1200 (Visual Studio 6.0 version 6.0)
MSVC++ 5.0 _MSC_VER == 1100 (Visual Studio 97 version 5.0)
It is important to note that the version numbers in the table refer to the major version of Visual Studio (e.g., "16.0" corresponds to Visual Studio 2019), not the year in the product name. A more comprehensive list can be found in relevant technical documentation.
Practical Application Example
Here is a simple C++ code example demonstrating how to use _MSC_VER for conditional compilation:
#include <iostream>
int main() {
#if _MSC_VER >= 1920
std::cout << "Compiling with Visual Studio 2019 or later" << std::endl;
#elif _MSC_VER >= 1910
std::cout << "Compiling with Visual Studio 2017" << std::endl;
#elif _MSC_VER >= 1900
std::cout << "Compiling with Visual Studio 2015" << std::endl;
#else
std::cout << "Compiling with an older version of Visual Studio" << std::endl;
#endif
return 0;
}
This example shows how to output different compilation messages based on the _MSC_VER value. In real projects, this technique is commonly used to enable or disable version-specific features.
Best Practices and Considerations
When detecting Visual Studio compiler versions, several important best practices should be followed:
- Use Version Ranges Instead of Exact Values: Starting with Visual Studio 2017, Microsoft began using monotonically increasing version number ranges. Therefore, it is recommended to check version ranges rather than exact compiler values. For example, use
#if _MSC_VER >= 1910instead of#if _MSC_VER == 1910. - Combine with _MSC_FULL_VER for Precise Matching: When exact matching of specific versions (such as Service Pack levels) is required,
_MSC_FULL_VERcan be used in combination. However, note that over-reliance on exact values may reduce code compatibility. - Refer to Boost Library Implementations: The
visualc.hppfile in the Boost library provides rich examples of version detection. These implementations are well-tested and can serve as references for real projects. - Command-Line Verification: In addition to detecting versions in code, you can also view compiler version information by running the
cl.exe /?command. For example:
c:\program files (x86)\microsoft visual studio 11.0\vc\bin>cl /?
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x86
Conclusion
Accurately detecting Visual Studio compiler versions is a crucial skill in C++ development. By appropriately using the _MSC_VER and _MSC_FULL_VER macros, developers can write more compatible and maintainable code. The version mapping table, code examples, and best practices provided in this article offer comprehensive guidance for implementing version detection in real projects. As Visual Studio continues to evolve, developers are advised to consult official documentation for the latest version information.