I am wondering why my use of swprintf_s is generating the
linker warning warning: implicit declaration of function 'swprintf_s'
I have this headers:
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <windows.h>
#include <tchar.h>
And
LPCWSTR foo = L"É %s";
LPCWSTR arg = "bá";
LPCWSTR msg = malloc(wcslen((wchar_t*)foo) + wcslen((wchar_t*)arg) + 1);
swprintf_s(msg, (wchar_t*)foo, (wchar_t*)arg);
How can I fix this?
All of the
_sfunctions (swprintf_s,memcpy_s) are Microsoft additions. They are part of Microsoft’s C library. When you use GCC, you will end up using a different C library, which doesn’t include Microsoft’s additions (but may include its own additions). Microsoft calls their C library the “CRT” or “C runtime”, GCC users refer to it as “libc” or even “glibc” (which is a specific implementation).If you look at a list of functions in Microsoft’s standard C library (MSDN docs), with a sharp eye you’ll notice a fairly large number of non-standard functions. Most of these are part of Microsoft’s “security enhancements” (MSDN docs). In general, I recommend avoiding the security enhancements since they are non-portable and they are not necessary for writing safe code.
Fix: You can use
swprintfinstead ofswprintf_s. Theswprintffunction is part of the C standard so it is present on many implementations (although it should be avoided on non-Windows platforms, since it useswchar_t).Note that
LPCWSTRis just a fancy typedef forconst wchar_t *, so you don’t need to cast it towchar_t *.