[OT] - Microsoft Madness: "strcpy_s" (And Friends) Does Not Compile On Linux - Help!


I loaded my SDL2 game into Visual Studio 2019 but it would not compile.
It complained about “strcpy” (and friends).

I fixed all the errors and it builds on Visual Studio.
Only issue is it won’t build on Linux now?

“strcpy_s” (and friends) does not work on Linux?
Any ideas on how to fix the above, thanks!


Annex K, which the _s functions come from is only supported on Windows so far. (And it’s not 100% standard compliant, but that’s another detail)

For most of them you could just make a wrapper using the preprocessor, but some of them get kind of tough to deal with. The most notable for me is the scanf family of functions that requires you to pass the length of strings right after them.

The two options you have are:

  • Use SDL’s libc if you have SDL in your project.
  • get rid of the _s warnings for Windows.

The latter you can be achieve by putting:

  • add_definitions​(-D_CRT_SECURE_NO_WARNINGS) ​#​ Disables warnings on _s functions (4996)
    In your CMakeLists.txt, or
  • #define _CRT_SECURE_NO_WARNINGS before any include of stdio.h

The option to make a “wrapper” would look like such:

#define __STDC_WANT_LIB_EXT1__ 1 //Not necessary on Windows, but technically required by standard

#include <stdio.h>

#if __STDC_LIB_EXT1__
#define MYFUNC(x, y, ...) myfunc_s(x, y, __VA_ARGS__)
#define MYFUNC(x, y, ...) myfunc(x, y, __VA_ARGS__)

You’d want to put this in its own header and import that in every file that needs stdio.h.

Hope that was helpful!

As Petruska said, use SDL’s libc functions instead. If you can replace all your VC runtime library functions with SDL’s own then you don’t have to have a dependency on the horrible VC DLLs.

Have a look at SDL_strlcpy.


I have three functions that do not build on Linux:

  1. strcpy_s → SDL_strlcpy
  2. strcat_s → ???
  3. sprintf_s → ???

What should I use for the other two to have one code building on Windows and Linux?


SDL_snprintf < you just have to add a maxlen, like you did with SDL_strlcpy.

SDL_strlcat < again with the maxlan, same as strlcpy.

SDL provides a pretty extensive replacement of the std stuff. Have a look at SDL_stdinc.h for a fill list.

Are these 3 functions in the SDL2 Wiki somewhere?

There’s more than 3, about 100, all replacements of these good old stdio/stdlib C functions. Unfortunately they’re not picked up in the Wiki. Best place to go is the header file I mentioned (/SDL2/include/SDL_stdinc.h). If you want to find out what they all do then google the part without “SDL_”. So if you wanted to know how SDL_atoi works, google “atio”. A trick that works in Visual Studio is if you start typing “SDL_” and then the function you want, it will give you a list of matches in knows (picked up from the project’s header files) while you type, including the parameters it takes.