1

I am attempting to compile/link my file named test.cpp from the command line using VS2017 on a 64-bit Win10 system. I use these two commands:

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
cl test.cpp

and I get the following results:

C:\temp\tests>"C:\Program Files (x86)\Microsoft Visual 
Studio\2017\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.4.5
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\temp\tests>cl test.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.11.25547 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
test.cpp(2): error C2365: 'y1': redefinition; previous definition was 'function'
C:\Program Files (x86)\Windows 
Kits\10\include\10.0.16299.0\ucrt\corecrt_math.h(984): note: see declaration of 'y1'
test.cpp(5): error C2659: '=': function as left operand

My file contains the following code:

#include <new>
static int *y1;
int main()
{
   y1 = new (std::nothrow) int;
}

While I understand why this is happening and any number of kluges that will fix it, it seems to me that the actual defect is in the Microsoft corecrt_math.h header file. When I look there I see several more identifiers (j0, jn, jn, etc.) that could just as easily be legitimately chosen by an unsuspecting programmer, only to end up with the same problem. I was under the impression that there was a "gentlemen's agreement" that exposed undocumented identifiers in library code would start or end with an underbar and user code would not, just to avoid this type of problem.

5
  • 3
    Is there a question hidden in there somewhere
    – ricco19
    Commented May 11, 2018 at 23:12
  • Bessel functions etc. were introduced in C++17. They should only be available via <cmath>, not via <math.h>. Of course, considering history, that was not such a realistic requirement, it was IMHO stupid. OH. Microsoft introduced some of these with simpler names at least back in VS 2015. Still from the docs it looks like they have leading underscore? Commented May 11, 2018 at 23:12
  • I've reproduced the problem with Visual C++ 2017. Commented May 11, 2018 at 23:16
  • y1 is also a POSIX.1-2001 function specified to be in <math.h>. But it's interesting it would end up indirectly included from <new>.
    – aschepler
    Commented May 11, 2018 at 23:17
  • 1
    Seems like a bug report, not a question. Microsoft should be informed.
    – ricco19
    Commented May 11, 2018 at 23:19

1 Answer 1

4

This build command works for me:

cl foo.cpp /Feb /D _CRT_DECLARE_NONSTDC_NAMES=0
4
  • That's a nice Nice find. Commented May 11, 2018 at 23:28
  • 1
    Here is an explanation from Steve Wishnousky at MSFT. TLDR it's compliant because the function name is reserved by POSIX. It may be better to just rename on your end.
    – ricco19
    Commented May 11, 2018 at 23:31
  • Great answer. When I examined the MSFT file a little closer I noticed that macro and was about to try disabling it just to see what would happen, then I saw this posting. My only concern is if disabling it might break something in some existing or future non-POSIX code. Commented May 12, 2018 at 1:05
  • @BenevolentDeity: I wasn't aware of it until this question. But just searching the VC++ include files shows that it controls inclusion of a great many no-leading-underscore macros and functions. I'll be defining it as 0 from now on. Commented May 12, 2018 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.