简体   繁体   English

如何使用VS2017编译较旧的C ++标准?

[英]How to compile with VS2017 for an older C++ standard?

I am using Visual Studio 2017 and need to create code that is compatible to VS2008 (C++03 or C++98). 我正在使用Visual Studio 2017,并且需要创建与VS2008兼容的代码(C ++ 03或C ++ 98)。 Is there a switch to restrict MSVC to C++03 features? 是否有将MSVC限制为C ++ 03功能的开关?

I am using CMake and tried to set 我正在使用CMake并尝试设置

set_property(TARGET tgt PROPERTY CXX_STANDARD 98)

But this seems only to make sure, that the compiler supports C++98 or newer. 但这似乎只能确保编译器支持C ++ 98或更高版本。

Any solution, that checks if C++ code uses features that are newer than the features supported by VS2008 will work as well. 任何可以检查C ++代码是否使用比VS2008支持的功能更新的功能的解决方案也可以使用。 I just need to make sure, that I do not accidentally use features that are too new. 我只需要确保我不会意外使用太新的功能。

MSVC only got the standard switch in one of the updates to VS2015 (Update 3 to be exact) which was more or less C++14 compliant, and as such there are only switches for standards starting with C++14 (plus a few later features that were already implemented at the time of the update). MSVC仅在VS2015的更新之一(确切地说是更新3)中获得了标准开关,该更新或多或少与C ++ 14兼容,因此,只有从C ++ 14开始的标准开关(加上一些)。更新时已实现的最新功能)。 All older features are enabled unconditionally for backwards compatibility (and because of all the work required to retrofit already implemented features for previous standards for virtually no gain). 无条件地启用了所有较旧的功能,以实现向后兼容(并且由于进行所有工作以对先前的标准进行改造,而实际上却没有任何收益)。

See this blog post for more information: https://devblogs.microsoft.com/cppblog/standards-version-switches-in-the-compiler/ 有关更多信息,请参见此博客文章: https : //devblogs.microsoft.com/cppblog/standards-version-switches-in-the-compiler/

Also, note that there were a lot of conformance improvements in newer versions of MSVC, so even with the std switches you could write code that wouldn't work or would behave differently on older compiler. 另外,请注意,新版本的MSVC中有很多一致性改进,因此即使使用std开关,您也可以编写在旧编译器上不起作用或表现不同的代码。

A better solution would be just to use VS2008 toolset from VS2017 visual studio, as explained here: https://devblogs.microsoft.com/cppblog/stuck-on-an-older-toolset-version-move-to-visual-studio-2015-without-upgrading-your-toolset/ 更好的解决方案是仅使用VS2017 visual studio中的VS2008工具集,如此处所述: https ://devblogs.microsoft.com/cppblog/stuck-on-an-older-toolset-version-move-to-visual-studio -2015-没有-升级,您的工具集/

That way you'll be certain your code compiles on the older toolset, while using up-to-date IDE. 这样,您可以确定在使用最新IDE的同时,您的代码可以在较旧的工具集上进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM