Undefined symbol: __ms_vsnprintf when linking with C code lazarus / 34-Windows ---------------------------------------------------------------------- alex256 · 2017-01-21 17:04 Hello. I'm trying to link my FPC program with C code and I get a linker message: Undefined symbol: __ms_vsnprintf I compiled my C code with MinGW-w64. To link it, I used the following directives: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         {$linklib libkernel32}    {$linklib libmsvcr100}    {$linklib libmsvcrt}    {$linklib libcrtdll}    {$linklib libgcc}  I wonder why can this thing happen and how to fix it. ---------------------------------------------------------------------- Thaddy · 2017-01-21 17:06 http://mingw-users.narkive.com/lxdyxcXN/missing-symbol-ms-vsnprintf- when-linking-with-cross-compiled-library According to this it was fixed some time ago: https://sourceforge.net/p/mingw-w64/mailman/mingw-w64- public/thread/CALc40c_- sXP0EK_+7fmCwXrPcDs3Dm1TM67g6bHRgc8HeR+nfA@mail.gmail.com/ How old is your mingw (especially the inc's)? ---------------------------------------------------------------------- alex256 · 2017-01-21 17:17 I used the latest version of MinGW-w64 (6.3.0). ---------------------------------------------------------------------- Thaddy · 2017-01-21 17:26 This seems not an FPC or Lazarus issue, but a mingw issue. Better ask there. It is a known issue if I google on it. ---------------------------------------------------------------------- alex256 · 2017-01-21 17:33 Thanks. ---------------------------------------------------------------------- alex256 · 2017-01-21 17:57 It may sound strange but I linked it for Win32 compiling my C code with MinGW and linking with libs from 32-bit version of MinGW-w64! Still wonder how to link it for Win64. ---------------------------------------------------------------------- Thaddy · 2017-01-21 19:43 The anwser is probably that the mingw64 includes are not correct. Try uninstalling mingw64 and re-install it with the correct headers from the install window. mingw64 is a bit misleading name. But this is not a mingw forum. I use it, but my core development (for fpc purposes) is on cygwin64, so I can't help any further. Maybe try cygwin64? ---------------------------------------------------------------------- alex256 · 2017-01-22 07:56 I tried to use cygwin64's libs and compile my C code in cygwin64. It linked fine, but the program crashes when I run it. Used the following directives: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$linklib c}{$linklib libgcc}{$linklib libcygwin}{$linklib libkernel32} ---------------------------------------------------------------------- Thaddy · 2017-01-22 08:40 If this is your first 64 bit version of your code, review the code for 64 bit/32 bit sizeint issues. The FPC side of the code will warn you with a "not portable" warning. The C side can also be made to emit such warnings. The fact that it now links is proof that the linker can at least resolve all entries. It is probably just a Native pointer or Native Integer size issue. Or a calling convention issue. ---------------------------------------------------------------------- DonAlfredo · 2017-01-22 08:55 And use Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$PACKRECORDS C}!! ---------------------------------------------------------------------- Thaddy · 2017-01-22 09:42 Indeed. {$packrecords C}  is essential. It may even be enough. Otherwise check for my suggestions. ---------------------------------------------------------------------- alex256 · 2017-01-22 10:19 Quote from: DonAlfredo on January 22, 2017, 09:55:47 amAnd use Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$PACKRECORDS C}!! I used it. The crash is in the C function which takes no parameters. I call it in the unit initialization. Assembler window says that the crash is in the freopen_r function (I call freopen from that C function). ---------------------------------------------------------------------- alex256 · 2017-01-22 10:20 Quote from: Thaddy on January 22, 2017, 09:40:41 amIt is probably just a Native pointer or Native Integer size issue. Or a calling convention issue. Both from FPC and C sides functions are called as cdecl. ---------------------------------------------------------------------- Thaddy · 2017-01-22 11:20 For the MS libraries that is most likely wrong on win64. Should be winapi or stdcall. win64 does not know cdecl. It knows only one calling convention. winapi selects the correct one on ms compilers based on a macro, cdecl too, but not on gnu. Note third party code can still be using cdecl, though. MS provided code is ONLY cdecl if the headers for those libraries expressly say so. ---------------------------------------------------------------------- alex256 · 2017-01-22 12:05 I changes cdecl to stdcall in both FPC and C sides, but still have the same crash. ---------------------------------------------------------------------- DonAlfredo · 2017-01-22 12:28 The following might or might not help. When trying to static include sqlite3 into the mORMot on win64, a crash happened during init when sqlite3 was compilied with optimalization level >=2 (with gcc). Going back to 1 or 0 did solve the crash. Again, it might help. Or not. And I do not know if you are able to recompile your lib. I think it has to do with thread init. But I have not tracked it down till now. ---------------------------------------------------------------------- alex256 · 2017-01-22 16:38 Quote from: DonAlfredo on January 22, 2017, 01:28:49 pmThe following might or might not help. When trying to static include sqlite3 into the mORMot on win64, a crash happened during init when sqlite3 was compilied with optimalization level >=2 (with gcc). Going back to 1 or 0 did solve the crash. Again, it might help. Or not. And I do not know if you are able to recompile your lib. I think it has to do with thread init. But I have not tracked it down till now. I'm able to recompile my library, but I compiled it without any optimizations. ---------------------------------------------------------------------- Thaddy · 2017-01-22 17:35 Try adding Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$ifdef win64} var    __ms_vsnprintf:pointer = nil;{$endif} before the {$linklib}'s The actual function prototype is __ms_vsnprintf = function(s:Pchar; n:size_t; const format:Pchar):integer;varargs;stdcall; ---------------------------------------------------------------------- alex256 · 2017-01-22 17:51 Quote from: Thaddy on January 22, 2017, 06:35:46 pmTry adding Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$ifdef win64} var    __ms_vsnprintf:pointer;{$endif} before the {$linklib}'s It doesn't work  The same linker error. ---------------------------------------------------------------------- Thaddy · 2017-01-22 18:42 Next trick. Let's force it to be defined. Compile this and Link this before any other library and see what happens. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i library vp;// For WIN64 only, otherwise cdecluses unixtype;type    Tvsnprintf = function(s:Pchar; n:size_t; const format{array of char}):integer;varargs;stdcall; //win64 has no cdecl!, just stdcall (or a winapi macro to stdcall)var  __ms_vsnprintf:Tvsnprintf = nil;export;beginend.  That should solve the linker issue, but of course the function can not be called. I suspect it is in msvcrt.dll or in the proprietary safestring library. by MS. (That's not a dll but a static library, it comes with the SDK, however and also with all versions of visual studio including the community edition) ---------------------------------------------------------------------- alex256 · 2017-01-23 16:23 I did this, but when I run my program, I get error 0xc000007b.  I'll better try to link my C library dynamically with the program... ---------------------------------------------------------------------- alex256 · 2017-01-23 16:33 I put my C code in DLL and it works! ---------------------------------------------------------------------- Thaddy · 2017-01-23 17:13 Good!. Funny thing is  discovered that MY version of Cygwin64 AND Mingw64 both contain this  __ms_vsnprintf routine in the includes and libraries, so there may still be something wrong with your setup? (Is is properly declared as external in your Pascalcode?)  OTOH if it works leave it alone  There's more MS code that only seems to works from a dll anyway. ---------------------------------------------------------------------- alex256 · 2017-01-23 17:58 Quote from: Thaddy on January 23, 2017, 06:13:24 pmGood!. Funny thing is  discovered that MY version of Cygwin64 AND Mingw64 both contain this  __ms_vsnprintf routine in the includes and libraries, so there may still be something wrong with your setup? (Is is properly declared as external in your Pascalcode?)  OTOH if it works leave it alone  There's more MS code that only seems to works from a dll anyway. What installation do you use?