Recent

Author Topic: [solved] *.dylib and "initialization" section under Mac  (Read 13158 times)

Frantic

  • New Member
  • *
  • Posts: 18
[solved] *.dylib and "initialization" section under Mac
« on: January 09, 2011, 01:00:15 pm »
Hi all!

I'm trying to make a *.dylib with FPC/Lazarus for Mac and use it from Objective-C code. How can I do that correctly?

The way I do that now is compiling *.dylib with "exports" section, which lists all functions I want to export (all declared as "cdecl"). Next, I just drag&drop *.dylib file into "Frameworks" section of Xcode project and create *.h file with exact same declaration as in my lazarus sources. When I run app from Xcode my *.dylib just happens to be loaded and I can call exported functions from it.

But this way has one big problem - "initialization" sections of all units (including system ones such as Classes, SysUtils, CThreads and so on) don't get called. Why? Can anyone help me with that?
« Last Edit: January 12, 2011, 12:19:37 am by Frantic »

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1071
Re: *.dylib and "initialization" section under Mac
« Reply #1 on: January 09, 2011, 03:18:28 pm »
Hi all!

I'm trying to make a *.dylib with FPC/Lazarus for Mac and use it from Objective-C code. How can I do that correctly?

The way I do that now is compiling *.dylib with "exports" section, which lists all functions I want to export (all declared as "cdecl"). Next, I just drag&drop *.dylib file into "Frameworks" section of Xcode project and create *.h file with exact same declaration as in my lazarus sources. When I run app from Xcode my *.dylib just happens to be loaded and I can call exported functions from it.

That's the correct way to do it.

Quote
But this way has one big problem - "initialization" sections of all units (including system ones such as Classes, SysUtils, CThreads and so on) don't get called. Why?

I don't know, because all tests in the FPC testsuite that check whether the unit initialisation sections are run for dynamic libraries work correctly for Mac OS X. You don't have to do anything special for that either.

Frantic

  • New Member
  • *
  • Posts: 18
Re: *.dylib and "initialization" section under Mac
« Reply #2 on: January 10, 2011, 12:55:47 pm »
Thank you for your reply. But somehow "initialization" doesn't work for me. Where can I find examples/testcases on Lazarus dylib Mac usage?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12645
  • FPC developer.
Re: *.dylib and "initialization" section under Mac
« Reply #3 on: January 10, 2011, 12:58:53 pm »
Try to call FPC_INITIALIZE and FPC_FINALIZE from C before and after usage.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1071
Re: *.dylib and "initialization" section under Mac
« Reply #4 on: January 10, 2011, 01:12:12 pm »
Thank you for your reply. But somehow "initialization" doesn't work for me. Where can I find examples/testcases on Lazarus dylib Mac usage?

I can't think of any reason why things would be different with Lazarus compared to without Lazarus.

The FPC test can be found here (you need all mentioned files):
* http://svn.freepascal.org/svn/fpc/trunk/tests/webtbs/tw8730a.pp
* http://svn.freepascal.org/svn/fpc/trunk/tests/webtbs/uw8730a.pp
* http://svn.freepascal.org/svn/fpc/trunk/tests/webtbs/tw8730b.pp
* http://svn.freepascal.org/svn/fpc/trunk/tests/webtbs/uw8730b.pp
* http://svn.freepascal.org/svn/fpc/trunk/tests/webtbs/tw8730c.pp

Compile the tw* files in alphabetical order, then run tw8730c. It will print an error message if something went wrong.

Also, which FPC version are you using?

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1071
Re: *.dylib and "initialization" section under Mac
« Reply #5 on: January 10, 2011, 01:15:38 pm »
Try to call FPC_INITIALIZE and FPC_FINALIZE from C before and after usage.

These are the wrong names (they are internal aliases for the standard Delphi routines initialize() and finalize()), but in general please do not try to manually initialise the RTL using hacks like that.

In the best case, things will work partially and only the initialisation code of the library source file itself won't be run. In the worst case, things will start crashing. It will only make debugging the actual problem harder.

Frantic

  • New Member
  • *
  • Posts: 18
Re: *.dylib and "initialization" section under Mac
« Reply #6 on: January 10, 2011, 09:19:57 pm »
I built FPC from SVN: Free Pascal Compiler version 2.5.1 [2010/11/16] for i386
but also tried on Free Pascal Compiler version 2.4.2rc1 [2010/11/16] for i386


Code: [Select]
$ ./tw8730c
Segmentation fault

I had seen this error before (when developing my own dylib) and added following code to library files:
Code: [Select]
library ...

...

begin
  Writeln('');
end.

After this I get following error:
Code: [Select]
$ ./tw8730c
result of function Lib2Func
tw8730c did not complete successfully

marcov, I tried to call it, but seams like there is no such function exported...
« Last Edit: January 10, 2011, 09:25:55 pm by Frantic »

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1071
Re: *.dylib and "initialization" section under Mac
« Reply #7 on: January 11, 2011, 09:47:27 am »
I built FPC from SVN: Free Pascal Compiler version 2.5.1 [2010/11/16] for i386
but also tried on Free Pascal Compiler version 2.4.2rc1 [2010/11/16] for i386


Code: [Select]
$ ./tw8730c
Segmentation fault

I had seen this error before (when developing my own dylib) and added following code to library files:
Code: [Select]
library ...

...

begin
  Writeln('');
end.

After this I get following error:
Code: [Select]
$ ./tw8730c
result of function Lib2Func
tw8730c did not complete successfully

The correct fix is to compile all dylibs with -k-no_order_inits. See the note under "Mac OS X 10.6" compatibility at http://www.freepascal.org/down/i386/macosx-ftp.freepascal.org.var, or in /Developer/Documentation/Free Pascal Compiler/Getting Started.rtf
« Last Edit: January 11, 2011, 11:54:26 am by jmaebe »

Frantic

  • New Member
  • *
  • Posts: 18
Re: *.dylib and "initialization" section under Mac
« Reply #8 on: January 12, 2011, 12:19:01 am »
I added "-k-no_order_inits" to compiler params... and my problem was solved!
1 000 000 thanks to you, man! You saved me :)

 

TinyPortal © 2005-2018