[solved] *.dylib and "initialization" section under Mac lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- Frantic · 2011-01-09 12:00 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? ---------------------------------------------------------------------- Jonas Maebe · 2011-01-09 14:18 Quote from: Frantic on January 09, 2011, 01:00:15 pmHi 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. QuoteBut 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 · 2011-01-10 11:55 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 · 2011-01-10 11:58 Try to call FPC_INITIALIZE and FPC_FINALIZE from C before and after usage. ---------------------------------------------------------------------- Jonas Maebe · 2011-01-10 12:12 Quote from: Frantic on January 10, 2011, 12:55:47 pmThank 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 · 2011-01-10 12:15 Quote from: marcov on January 10, 2011, 12:58:53 pmTry 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 · 2011-01-10 20:19 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... ---------------------------------------------------------------------- Jonas Maebe · 2011-01-11 08:47 Quote from: Frantic on January 10, 2011, 09:19:57 pmI 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 ---------------------------------------------------------------------- Frantic · 2011-01-11 23:19 I added "-k-no_order_inits" to compiler params... and my problem was solved! 1 000 000 thanks to you, man! You saved me