Lazarus

Installation => macOS / Mac OS X => Topic started by: cai on June 25, 2021, 10:42:40 am

Title: is this an issue? export stdcall function in dynamic library on MAC OS
Post by: cai on June 25, 2021, 10:42:40 am
create a library project A

there is a function like this:

function func(): Integer;stdcall;
begin
  Result := 0;
end;

exports
  func;

-------------------------------
create an application B

use code like:
var
  func: function (): Integer;stdcall;
begin
  func = GetProcAddress(LoadLirary ('/libA.dylib'), 'func');
  if Assigned(func ) then
    ShowMessage('func  Assigned')
  else
    ShowMessage('func  not Assigned')
end;

run & show "func  not Assigned".
-------------------------------------------
if use cdcel instead of stdcall, will show "func  Assigned"
-------------------------------------------
or, if change exports code like this:
exports
  func name '_func';

also work!
Title: Re: is this an issue? export stdcall function in dynamic library on MAC OS
Post by: trev on July 25, 2021, 04:09:33 am
If you use "cdecl", FPC ensures that your function completely adheres to all ABI requirements (naming, parameter passing, etc). If you don't, then you are on your own.
Title: Re: is this an issue? export stdcall function in dynamic library on MAC OS
Post by: PascalDragon on July 26, 2021, 01:50:39 pm
Please note that stdcall is only relevant on i386. For all other targets stdcall is a synonym for cdecl. As macOS nowadays is 64-bit only, simply use cdecl.
Title: Re: is this an issue? export stdcall function in dynamic library on MAC OS
Post by: Wallaby on July 26, 2021, 02:44:53 pm
-------------------------------------------
if use cdcel instead of stdcall, will show "func  Assigned"
-------------------------------------------
or, if change exports code like this:
exports
  func name '_func';
also work!

One of the conventions (i.e. stdcall or cdecl) will add an underscore to the function's name. I am guessing that's coming from the linker (ld). Like others said, use cdecl, since stdcall is not designed for anything other than 32-bit Intel CPUs.
TinyPortal © 2005-2018