7

When building this very simple test program

#include <iostream>
int main() {
    std::cout << "x";
}

with visual studio 2019 and /Wall I'm getting a

warning C4668: '__STDC_WANT_SECURE_LIB__' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'

Trying to

#define __STDC_WANT_SECURE_LIB__ 0

before including iostream results in

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\xlocnum(1388,69): error C2039: 'sprintf_s': is not a member of '`global namespace''

at least for my VS. Godbolt doesn't complain.

#define __STDC_WANT_SECURE_LIB__ 1

is fine and doesn't let the compiler complain about sprintf_s which one would expect.

Microsoft doesn't show me any results when searching for it.. SO does here but overall i can't find many resources on if and how to use that define.

Is there a way to disable the secure extensions and include <iostream> ? Am i using the wrong define or approach for this?

jesses
  • 559
  • 3
  • 15
  • 1
    You can disable `C4668` for the whole translation unit(s) with [`/wd4668`](https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-160), or just for the std header(s) with `#pragma warning(push)` and [`#pragma warning(disable: 4668)`](https://learn.microsoft.com/en-us/cpp/preprocessor/warning?view=msvc-160) before `#include ` then `#pragma warning(pop)` after it. You cannot `#define __STDC_WANT_SECURE_LIB__ 0` because the implementation requires it internally, and that's probably the reason why it's not documented. – dxiv Feb 28 '21 at 21:04
  • @dxiv what can i do about the C2039 assuming not having the secure extensions is what i want? Or say, how to properly disable the secure extensions? – jesses Feb 28 '21 at 21:07
  • Guess you wrote it while I was adding the last sentence to my previous comment. Note that macros starting with a double underscore are reserved for compiler's internal use. You are not supposed to rely on them, let alone redefine them. – dxiv Feb 28 '21 at 21:09
  • Given it's an internal macro, any idea why it's not defined internally and hits this warning? Is there some other include required? – Brad Sep 13 '22 at 17:39

1 Answers1

0

I had the same error, don't have needed permission... blah-blah-blah

'STDC_WANT_SECURE_LIB' in VS Community 2022, with pre-compiled CMake SFML-2.6.x (!)

My mistake was putting the wrong path to folder "SFML-2.6.x\SFML_cmake_x64\lib\Debug" - I did not include folder "\Debug". And had the result: "SFML-2.6.x\SFML_cmake_x64\lib" -- that's wrong way!!!!!

Every time you must put the full path to folder 'lib' with \Debug or \Release version

avariant
  • 2,234
  • 5
  • 25
  • 33