__cplusplus apparently not set correctly in Visual Studio 2022 when building for C++17? - visual-c++

My code has the following test, to protect code that only works in C++11 or newer.
It is evaluating to 0 despite cl being invoked with /std:c++17 .
#if __cplusplus >= 201103
I am seeing this in Microsoft Visual Studio Community 2022 (64-bit) on Windows 11.

Right click on the project in question, and set Properties-->C/C++-->Command Line-->Additional Options with /Zc:__cplusplus
My best guess is that they instituted this bizarre mis-feature to service a "bug report" that code that was testing for the original value with == was failing once they increased the version number.
There is more information at https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170

Related

In MSVC 16.10.1, Intellisense and compiler disagree about __cplusplus ('201705' vs. '202004')

In MS Visual Studio 2019, Ver. 16.10.1 (or 16.11.0 Preview 1.0), Toolset v142
with Option /std:latest /Zc:__cplusplus
Intellisense tells me __cplusplus is ' 201705L',
but the compiled program prints the following:
"MSVC= '1929', __plusplus = '202004'":
Can someone please confirm that this is an error and tell me where and how I should/could best report something like this?
(
With the MS compiler currently available at Godbolt.org, this discrepancy does not occur. The compilation there says '201705', see https://godbolt.org/z/oax35erME.
)

"error C2228: left of '.ToString' must have class/struct/union"

So I recently got Visual Studio 2012. I converted a Visual Studio 2010 solution to a Visual Studio 2012 project. It was working before I converted it.
I have this line of code:
this->Text = global::ProjectName + " (" + global::Type.ToString() + ") - Path Creator 2.0";
where global::Type is:
ref class global {
public:
static Rct3PathType Type;
...
};
...and Rct3PathType is:
enum class Rct3PathType {
Basic = 0x02060206,
Extended = 0x05060506,
Queue = 0x01070107
};
I get an error at compile time where ever I have called global::Type.ToString() saying "error C2228: left of '.ToString' must have class/struct/union". Considering this all compiled completely well BEFORE switching to Visual Studio 2012, I'm not sure what the issue is! :(
Also, when I try to run the last successful build (which was built with Visual Studio 2010), I get runtime error at startup saying "The program can't start becayse MSVCR100D.dll is missing from your computer." I know this probably doesn't relate to the issue at hand, but does anyone know why this may be happening as well?
Thanks for your help,
Alex
"error C2228: left of '.ToString' must have class/struct/union": enum class is also the syntax for a C++11 enum. To make it a C++/CLI enum, give it an accessibility specifier, which is not allowed on a C++11 enum. In other words, private enum class or public enum class will change it from a C++11 enum to a C++/CLI enum. This wasn't a problem in VS2010 because it doesn't support C++11 enums.
"The program can't start because MSVCR100D.dll is missing from your computer.": VS2012 uses a different C runtime than VS2010. MSVCR100D is the Microsoft Visual C Runtime version 10.0, Debug version. This DLL is installed with VS2010, there is no other way to get it. If you have an old Release build, you can get the runtime redistributable from Microsoft (x86 or x64), and install that to make it run. (That will be MSVCR100.dll, no "D" at the end.)

Cannot Compile OpenCV 2.4.5 with VS 2013 RTM

Has anyone had any luck compiling openCV with VS 2013 RTM? I have tried and get a bunch of "min doesn't belong to namespace std" "max doesn't belong to namespace std" in the IlmImf module, and opencv_features2d doesn't compile with the following error:
opencv\modules\core\include\opencv2/core/core.hpp(4512): fatal error C1075: end of file found before the left brace '{' at '......\modules\features2d\src\features2d_init.cpp(187)' was matched
Since the latest CMake UI doesn't yet support building with 2013 (at least from the UI and I'm a noob), my process was configuring CMake for 2012, and then opening the generated solution with 2013 and upgrading the compiler to vc12.
I was able to get past the min/max errors by adding header includes for in the 'offending' files, but I am stumped by the full error I posted above.
Thanks
Update:
The accepted answer provides what is necessary to compile OpenCV in 32-bit debug and release, and 64-bit debug, but now the compiler fails to compile 64-bit release due to an internal compiler error. This is likely the compiler's fault at this point, but the answer is still solves many problems.
Update 2:
So the 64-bit issue turned out to be a bug in the auto-vectorizer. Here is the corresponding workaround.
Hi, thanks for the great bug report. I confirm this is a bug in the compiler optimizer. We will fix it in a future release.
If you need a source code workaround, please turn the vectorizer off on the loop inside computeOrbDescriptor:
#pragma loop(no_vector)
for (int i = 0; i < dsize; ++i)
{
**...**
That lets me build orb.cpp & stardetector.cpp.
If this issue is severe, causing critical business situations or blocking your product development or deployment, please go to http://support.microsoft.com or call 1-800-MICROSOFT for assistance. For Microsoft premier customers, please contact your administrator, your Technical Account Manager, or your Microsoft premier account representative.
I am closing this MSConnect item. Feel free to respond if you need anything else.
Thanks,
Eric Brumer - Microsoft Visual C++ Team
The connect bug can be found here. Unfortunately the real fix for this is postponed until a later date.
I've managed to compile OpenCV 2.4.6 on VS2013 RC, but initially it had the same errors as in question.
I've opened VS solution and fixed all error in two steps:
1) Replaced (Ctrl+H)
#include \<string\>
to
#include <algorithm>\n#include <string>
in entire solution (be sure to enable RegExp in replace dialog)
2) In "modules/opencv_features2d/Src/features2d_init.cpp" changed line 184 to:
obj.info()->addParam(obj, "detector", (Ptr<Algorithm>&) obj.detector);
(search for "GridAdaptedFeatureDetector" in this file for other OpenCV versions)
For the first issue:
http://blogs.msdn.com/b/vcblog/archive/2013/06/28/c-11-14-stl-features-fixes-and-breaking-changes-in-vs-2013.aspx
It is explicitly called out:
You must #include <algorithm> when calling std::min() or std::max().
Before due to the internal implementation of the VC++ libraries <string> would pull in these functions.
Please see http://code.opencv.org/issues/3273 for more information on the internal compiler error issue.

Why does Visual Studio 2012 Express's auto parallelization reporting feature only work with Win32?

I tried to use Microsoft Visual Studio 2012 Express to auto parallelize a "for" loop in Win 32 bit and x64 bit settings. Command line options were set to /O2 /Qpar /Qpar-report:2 to enable optimization, auto parallelization, and reporting of successful and failed "for" loop auto-parallelization. 32 to 64 bit settings were changed via the method used here: ( http://msdn.microsoft.com/en-us/library/vstudio/9yb4317s.aspx ). A 64 bit version of Windows is running on my computer. The sample code I used came from here: (http://msdn.microsoft.com/en-us/library/hh872235.aspx). The code I ran was as follows:
int A[1000];
void test()
{
___#pragma loop(hint_parallel(0))
___for (int i=0; i<1000; ++i)
___{
______A[i] = A[i] + 1;
___}
___for (int i=1000; i<2000; ++i)
___{
______A[i] = A[i] + 1;
___}
}
int main()
{
___test();
___return 0;
}
Building with Win32, I yield this reporting output:
--- Analyzing function: void __cdecl test(void)
d:\myproject\mytest.cpp(4) : loop parallelized
d:\myproject\mytest.cpp(4) : loop not parallelized due to reason '1008'
Building with x64, I did not yield any reports of "loop parallelized" or "loop not parallelized".
Why were there no reports? Is it because I only have visual studio 2012 express, but I needed VS 2012 professional? Does this happen to all computers or across all version of VS 2012? How do I fix this problem so that I will have auto-parallelization reporting ( /Qpar-report:2 ) on with a 64 bit Microsoft Visual Studios project?
/Qpar-report works well in x64 compilation mode in the retail edition. Express doesn't use a special build of the compiler.
A possible explanation is that you forgot to also set the /Qpar-report option for your x64 configuration. These settings are saved per configuration. Right-click your project, Properties. Check the combo boxes at the top of the dialog. Ensure that you've got the proper Configuration and Platform selected.
And make sure you do this for the Release build, the Debug build doesn't parallelize these loops.

Build Issues after Upgrading app from vs2005 to vs2012

I need my application to be upgraded from visual studio 2005 IDE to visual studio 2012 .
The upgradation wizard converts the solution and project files successfully with 0 errors and few warnings.
But when i start building the application i get error message :
error C1189: #error : This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x0501 or higher is recommended. in atlcore.h !
I tried changing the version no to 0x0500 , 0x0501 , 0x0502 and also 0x0601 ( both through /D compiler option and manually changing in atlcore.h , WINVER is also changed. ) but no luck . the same error is being displayed.
Where do i go wrong ?
Visual C++ no longer supports targeting Windows 95, Windows 98, Windows ME, or Windows NT. If your WINVER or _WIN32_WINNT macros are assigned to one of these versions of Windows, you must modify the macros.
To modify the macros, in a header file, add the following lines.
#define WINVER 0x0500
#define _WIN32_WINNT 0x0500
EDIT:
WINVER determines the minimum platform SDK required to build your application, which in turn will determine at compile time which routines are found by the headers.
#define _WIN32_WINNT_NT4 0x0400
#define _WIN32_WINNT_WIN2K 0x0500
#define _WIN32_WINNT_WINXP 0x0501
#define _WIN32_WINNT_WS03 0x0502
#define _WIN32_WINNT_WIN6 0x0600
#define _WIN32_WINNT_VISTA 0x0600
#define _WIN32_WINNT_WS08 0x0600
#define _WIN32_WINNT_LONGHORN 0x0600
#define _WIN32_WINNT_WIN7 0x0601
Other Solution:
If you have installed a WIndows SDK on your PC (in /Microsoft SDKs/Windows), you can #include in stdafx.h (or in a header you include in all your C++ files). Including SDKDDKVer.h will target the highest Windows version available.
Hopefully It work!!!!!
For more info SEE HERE
Problem temporarily solved by commenting a check in atlcore.h :
if _WIN32_WINNT > 0x0501
//#error This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x0501 or higher is recommended.
endif
I know it isnt the right way to do [ editing a file shipped by the IDE ] but did since it may be due to Improper installation.
If anyone come across a permanent fix let me know .
you can add a pre-processor directive for the project under project settings, C/C++, Pre-processor definitions, appending WINVER=0x0501;
(you can also undefine definitions)
I'm wondering if you are using pre-compiled headers which is overwriting changes to stdafx.h, this is the way to make sure this is set
This preprocessor setting holds until code in the project files changes it, at which point if this doesn't fix the problem, then you must find how or where this is being set/unset/checked; but the solutions shouldn't involve any changes to the windows SDK files

Resources