Forum > macOS / Mac OS X

How to port .dylib for dynamic linking

(1/3) > >>

CCRDude:
I tried both Google and a search here, but results were either not fitting or not working.

I'm using FPC + Lazarus trunk snapshots to develop. I cross-compile from Windows to Mac.

The project I'm working on consists of an executable and a dynamically linked library. Functions are exported stdcall, and dynamically imported during runtime using LoadLibrary and GetProcAddress.

On Windows (.dll) and Linux (.so), everything works fine. On Mac OS (High Sierra), LoadLibrary works (receives a handle), but GetProcAddress fails (returns nil).

I read about the recommendation to prefix the export with an underscore, but that did not help (seems to have been for static linking?).

nm -m -U libsomething.dylib returns:

--- Code: ---000000000003fd0 (__TEXT,__text) external MyFunctionName
...
0000000000003f80 (__TEXT,__text) external _MyFunctionName
--- End code ---

Any ideas what I can do to make the import successful?

Thaddy:

--- Quote ---Functions are exported stdcall,
--- End quote ---
On Mac and Linux the calling convention is most likely if not always cdecl. stdcall is mostly Win32 specific.(So not so standard as the name suggests  :o )

A way to solve this is like so:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// add on top of your import unit and *remove* all specific calling specifiers from the individual the imported methods{$ifdef mswindows}{$ifdef wince}{$calling cdecl}{$else}{$calling stdcall}{$endif}{$elsif unix}{$calling cdecl}{$else}{$calling default}{$endif}This is not complete but will cover the windows versions and unixes. (for win64 stdcall is actually ignored, wince is cdecl)

See https://www.freepascal.org/docs-html/current/prog/progsu7.html#x14-130001.2.7 and
https://www.freepascal.org/docs-html/current/prog/progse22.html#x173-1760006.3

CCRDude:
But both library and executable are from my source, so I know I've specified "stdcall" in both (should not be that important on the executable side, since GetProcAddress should(?) not be affected? ;)

Anyway, updated both library and executable to use cdecl (and learned more about using macros to still have stdcall on Windows), same result, GetProcAddress returns nil.

Thaddy:
You mean this (or something like this) macro?
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$macro on}{$if not defined(mswindows} {$define stdcall:=cdecl}{$endif}That is also possible but less complete. It is also a bit confusing if others use your code: all is not what it seems, you hide essential information.
Note in general it is always better to use the calling convention for the platform for any library including your own.

balazsszekely:

--- Quote ---Anyway, updated both library and executable to use cdecl (and learned more about using macros to still have stdcall on Windows), same result, GetProcAddress returns nil.

--- End quote ---
Right after GetProcAddress fails:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---ShowMessage(SysErrorMessage(GetLastOSError));It should work on OSX too. The obvious quess is that you don't use the full path to the library. Is the dynlib inside the bundle?

PS: Can you attach a demo application with a demo library which fails?

Navigation

[0] Message Index

[#] Next page

Go to full version