On Wed, Mar 9, 2011 at 03:32, Dock, Dion <dion_dock@mentor.com> wrote:

I'm back to my original question: can I use MinGW64 with source that has functions like strnlen_s?

It sounds like the answer is: I can compile code with those functions in a DLL with a manifest for msvcr80 (or 90 or 100) or statically link the runtime into that DLL.  However, I cannot compile code with those functions using MinGW64.

Or to put it another way, how would you get this to compile with MinGW64?
       #include <stdio.h>
       #include <string.h>

       int main()
          printf("%d\n", strnlen_s("foo", 2)); /* expect 2 */
          return 0;

Here's what I get:
C:\Temp>C:\mingw_32\bin\i686-w64-mingw32-gcc.exe main.c
C:\Users\dockd\AppData\Local\Temp\ccKqFt47.o:main.c:(.text+0x1e): undefined reference to `strnlen_s'
collect2: ld returned 1 exit status


It seems that you can compile your program, so you can copy msvcr100.dll to libmsvcrt.a, it should works fine.
Or link with: gcc test.c -lmsvcr100

But I checked the functions in msvcr100.dll, no strnlen_s, only have the following string functions:


When I check VC 10, I found it's a inline function:

#if __STDC_WANT_SECURE_LIB__ && !defined (__midl)
_Check_return_ static __inline size_t  __CRTDECL strnlen_s(_In_z_  const char * _Str, _In_ size_t _MaxCount)
    return (_Str==0) ? 0 : strnlen(_Str, _MaxCount);

_Check_return_ static __inline size_t __CRTDECL wcsnlen_s(_In_z_ const wchar_t * _Src, _In_ size_t _MaxCount)
    return (_Src == NULL) ? 0 : wcsnlen(_Src, _MaxCount);

So if you want use "secure" _s functions, you can link with -lmsvcr100, but please do not use strnlen_s/wcsnlen_s,
or you must define your self.