Json: error C2039: '_snprintf': is not a member of 'std' - Windows

Created on 30 Dec 2018  Â·  27Comments  Â·  Source: nlohmann/json

I used the latest release (single include file), compiled using VS2017 - Visual Studio 2017 Developer Command Prompt v15.9.1.

As a work around I removed std:: from all the snprintf calls.

I was building an addon for kodi: https://github.com/kodi-pvr/pvr.vuplus/pull/156

Full gist of build output: https://gist.github.com/phunkyfish/c8456619bd85c16b1f9243e83517ac46

Note that it builds for all other platforms without issue.

visual studio invalid

Most helpful comment

I experienced this issue with Visual Studio 2015. My application used Python as embedded scripting language, and pyerror.h redefines snprintf to _snprintf if configuration property HAVE_SNPRINTF is not defined. When I added HAVE_SNPRINTF definition to my project, json.hpp started to compile. So, it looks like Microsoft itself doesn't redefine snprintf, however, software that supports compatibility with older Visual Studio versions may create such definition, especially if it is configured in incorrect way.

So, when you have such issue, first of all you have to review other open source dependencies that you have in your project.

All 27 comments

This is strange, because the MSVC builds run without problems on AppVeyor, see https://ci.appveyor.com/project/nlohmann/json.

```
Microsoft (R) Program Maintenance Utility Version 14.16.27023.1
Copyright (C) Microsoft Corporation. All rights reserved.

d:\jenkinsworkspace\binary-addons\kodi-windows-x86_64-master\tools\depends\target\binary-addons\pvr.vuplus\src\enigma2\utilities/json.hpp(3930): error C2039: '_snprintf': is not a member of 'std'
d:\jenkinsworkspace\binary-addons\kodi-windows-x86_64-master\tools\depends\target\binary-addons\pvr.vuplus\src\enigma2\utilities/json.hpp(20292): note: see declaration of 'std'
d:\jenkinsworkspace\binary-addons\kodi-windows-x86_64-master\tools\depends\target\binary-addons\pvr.vuplus\src\enigma2\utilities/json.hpp(3921): note: while compiling class template member function 'std::string nlohmann::detail::lexer>::get_token_string(void) const'
d:\jenkinsworkspace\binary-addons\kodi-windows-x86_64-master\tools\depends\target\binary-addons\pvr.vuplus\src\enigma2\utilities/json.hpp(5020): note: see reference to function template instantiation 'std::string nlohmann::detail::lexer>::get_token_string(void) const' being compiled
d:\jenkinsworkspace\binary-addons\kodi-windows-x86_64-master\tools\depends\target\binary-addons\pvr.vuplus\src\enigma2\utilities/json.hpp(4967): note: see reference to class template instantiation 'nlohmann::detail::lexer>' being compiled
d:\jenkinsworkspace\binary-addons\kodi-windows-x86_64-master\tools\depends\target\binary-addons\pvr.vuplus\src\enigma2\utilities/json.hpp(13447): note: see reference to class template instantiation 'nlohmann::detail::parser>' being compiled
d:\jenkinsworkspace\binary-addons\kodi-windows-x86_64-master\tools\depends\target\binary-addons\pvr.vuplus\src\enigma2\utilities/json.hpp(20307): note: see reference to class template instantiation 'nlohmann::basic_json' being compiled
NMAKE : fatal error U1077: 'C:\PROGRA~2MIB055~12017COMMUN~1\VC\ToolsMSVC\1416~1.270\bin\Hostx64x64\cl.exe' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)Microsoft Visual Studio2017Community\VC\ToolsMSVC\14.16.27023\bin\HostX64x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)Microsoft Visual Studio2017Community\VC\ToolsMSVC\14.16.27023\bin\HostX64x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)Microsoft Visual Studio2017Community\VC\ToolsMSVC\14.16.27023\bin\HostX64x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)Microsoft Visual Studio2017Community\VC\ToolsMSVC\14.16.27023\bin\HostX64x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)Microsoft Visual Studio2017Community\VC\ToolsMSVC\14.16.27023\bin\HostX64x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)Microsoft Visual Studio2017Community\VC\ToolsMSVC\14.16.27023\bin\HostX64x64\nmake.exe"' : return code '0x2'
Stop.
Archiving artifacts
‘cmake/addons/build/zips/pvr.vuplus+windows-x86_64/pvr.vuplus-.zip’ doesn’t match anything: ‘cmake’ exists but not ‘cmake/addons/build/zips/pvr.vuplus+windows-x86_64/pvr.vuplus-.zip’
No artifacts found that match the file pattern "cmake/addons/build/zips/pvr.vuplus+windows-x86_64/pvr.vuplus-*.zip". Configuration error?```

Might it be the version: Utility Version 14.16.27023.1

It seems that Microsoft somewhere uses a macro and forwards snprintf to _snprintf and then the resulting code std::_snprintf makes no sense. I'll check what I can do.

Json CPP had similar problem.

https://github.com/open-source-parsers/jsoncpp/issues/224

Maybe you just need to add:

#if defined(_MSC_VER) && _MSC_VER < 1500 // VC++ 8.0 and below
#define snprintf _snprintf

To support version earlier than 15

Sigh... Yet another instance where I need to "patch" C++11 code such that it can compile with the most recent Microsoft compiler...

Unfortunately that’s not uncommon.

Are you sure this definition is from VC++ itself and not some other library that you're including?

I’m not sure at all. While developing I build the addon on Mac which doesn’t have the issue. The windows build happens on the automated build system for the addon.

Are you sure this definition is from VC++ itself and not some other library that you're including?

I also had this problem, but the query found that other libraries defined it. I am also very upset now.

This is tricky, my guess is the occurrences of this will rise with adoption and it will be difficult to define to users what the correct workaround is.

@1996sc30 You are saying that the define came from another library, but not this one? Meaning the error has nothing to do with this library?

@1996sc30 You are saying that the define came from another library, but not this one? Meaning the error has nothing to do with this library?

YesIndeed so.

@phunkyfish Can you confirm @1996sc30 's observation?

Let me find who to ask and I will bring them in on this thread.

Should the code be compatible with utility version 14.16.27023.1?

@1996sc30 can you confirm what compiler and utility version you are using? Also which was the offending library that caused this problem?

@phunkyfish I do not understand the question.

Let me find who to ask and I will bring them in on this thread.

Should the code be compatible with utility version 14.16.27023.1?

@1996sc30 can you confirm what compiler and utility version you are using? Also which was the offending library that caused this problem?

I am using VS2015 and using nlohmann/json as the latest version, the conflict is the other library(evpp—https://github.com/Qihoo360/evpp) I used in the project .

So it seems we can close this issue?

Yes, thanks!

I experienced this issue with Visual Studio 2015. My application used Python as embedded scripting language, and pyerror.h redefines snprintf to _snprintf if configuration property HAVE_SNPRINTF is not defined. When I added HAVE_SNPRINTF definition to my project, json.hpp started to compile. So, it looks like Microsoft itself doesn't redefine snprintf, however, software that supports compatibility with older Visual Studio versions may create such definition, especially if it is configured in incorrect way.

So, when you have such issue, first of all you have to review other open source dependencies that you have in your project.

Yes, the solution in my case was to undef in my app to allow the standard declaration to work.

I'm not sure why this is closed as out of scope. Any program that includes nlohmann/json.hpp may run into this error, because that file references std::snprintf. Would it not be better to #undef snprintf there, rather than in potentially every file that includes it? Or could that cause other issues?

Would it not be better to #undef snprintf there, rather than in potentially every file that includes it? Or could that cause other issues?

I don't think so. This would be as surprising for users of the library as other libraries introducing such macros in the first place. std::snprintf is C++11 standards-compliant, and whoever has issues compiling that, should take care of fixing the issue.

std::snprintf is C++11 standards-compliant, and whoever has issues compiling that, should take care of fixing the issue.

Fair point. :-)

you can add “#undef snprintf” At the head of the file json.hpp

No, it’s not the library‘s job to clean up such names.

Agreed that users of C++ libraries that #define snprintf should bear the burden of cleaning up after the non-compliant library, and hopefully getting the library authors to fix it themselves. They can create a simple wrapper header that includes the offending header and then does the #undef snprintf.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Prati369 picture Prati369  Â·  4Comments

SimplyLiz picture SimplyLiz  Â·  3Comments

asmaloney picture asmaloney  Â·  4Comments

zhishupp picture zhishupp  Â·  4Comments

Fonger picture Fonger  Â·  4Comments