Recent

Author Topic: Undefined symbol: __ms_vsnprintf when linking with C code  (Read 10464 times)

alex256

  • New Member
  • *
  • Posts: 24
Undefined symbol: __ms_vsnprintf when linking with C code
« on: January 21, 2017, 06:04:02 pm »
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]
  1.    
  2.     {$linklib libkernel32}
  3.     {$linklib libmsvcr100}
  4.     {$linklib libmsvcrt}
  5.     {$linklib libcrtdll}
  6.     {$linklib libgcc}
  7.  

I wonder why can this thing happen and how to fix it.

Thaddy

  • Hero Member
  • *****
  • Posts: 9303
« Last Edit: January 21, 2017, 06:10:19 pm by Thaddy »
also related to equus asinus.

alex256

  • New Member
  • *
  • Posts: 24
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #2 on: January 21, 2017, 06:17:10 pm »
I used the latest version of MinGW-w64 (6.3.0).

Thaddy

  • Hero Member
  • *****
  • Posts: 9303
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #3 on: January 21, 2017, 06:26:47 pm »
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.
also related to equus asinus.

alex256

  • New Member
  • *
  • Posts: 24
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #4 on: January 21, 2017, 06:33:55 pm »
Thanks.

alex256

  • New Member
  • *
  • Posts: 24
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #5 on: January 21, 2017, 06:57:21 pm »
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

  • Hero Member
  • *****
  • Posts: 9303
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #6 on: January 21, 2017, 08:43:29 pm »
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?
also related to equus asinus.

alex256

  • New Member
  • *
  • Posts: 24
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #7 on: January 22, 2017, 08:56:26 am »
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]
  1. {$linklib c}
  2. {$linklib libgcc}
  3. {$linklib libcygwin}
  4. {$linklib libkernel32}
  5.  

Thaddy

  • Hero Member
  • *****
  • Posts: 9303
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #8 on: January 22, 2017, 09:40:41 am »
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.
also related to equus asinus.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1171
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #9 on: January 22, 2017, 09:55:47 am »
And use
Code: Pascal  [Select]
  1. {$PACKRECORDS C}
!!

Thaddy

  • Hero Member
  • *****
  • Posts: 9303
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #10 on: January 22, 2017, 10:42:11 am »
Indeed. {$packrecords C}  is essential. It may even be enough.
Otherwise check for my suggestions.
also related to equus asinus.

alex256

  • New Member
  • *
  • Posts: 24
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #11 on: January 22, 2017, 11:19:32 am »
And use
Code: Pascal  [Select]
  1. {$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

  • New Member
  • *
  • Posts: 24
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #12 on: January 22, 2017, 11:20:59 am »
It 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

  • Hero Member
  • *****
  • Posts: 9303
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #13 on: January 22, 2017, 12:20:31 pm »
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.
« Last Edit: January 22, 2017, 12:31:06 pm by Thaddy »
also related to equus asinus.

alex256

  • New Member
  • *
  • Posts: 24
Re: Undefined symbol: __ms_vsnprintf when linking with C code
« Reply #14 on: January 22, 2017, 01:05:06 pm »
I changes cdecl to stdcall in both FPC and C sides, but still have the same crash.