Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2014-08

Building a static library

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Sergio Flores

Let's start this email by saying that I know perfectly that FPC does not support static libraries, I read all I could find about it. However most reasons talk about that there would be problems if someone tried to use two fpc static libs together because of duplicated symbols.

In my case I want to create a iPhone static lib to be used by objective-c and C++ users. Since in this special case there will be one and only one lib written in pascal, I though this would make it possible.

I saw that my own iOS projects that were based on templates worked by compiling a static libFPC.a that would be then called by Objective-C.

So I did create my own static library by gathering all .o files from FPC into a .a file. I wrote a .h header and added the static lib into a Xcode Objective-C project.
Ok, it compiles and links correctly. It calls some of the pascal functions correctly. But.. then it later crashes in one of the pascal functions.

Note that I have a lib_init() function called from C that is used to initialize the pascal library, and it calls this undocumented fpc function:

procedure internal_initializeunits(); external name 'FPC_INITIALIZEUNITS';

This function from what I understood it calls all units initialization sections, right?
Still, I'm missing something... the libFPC.a compiled in xcode templates works fine with Objective-C, but also it does not share any memory back or forward.

This seems to be some problem with memory allocation, since only calling any function that creates pascal strings or classes seem to crash the app. Any clue?

Reply by email

Vsevolod Alekseyev
to Sergio Flores

I've got a static FPC library on iOS that works fine. Don't pass strings back and forth, pass PChar's. Pointer-to-records are OK, too, but I'd think twice regarding objects.

Sergio Flores wrote 41 lines
Let's start this email by saying that I know perfectly that FPC does not support static libraries, I read all I could find about it. However most reasons talk about that there would be problems if someone tried to use two fpc static libs together because of duplicated symbols. In my case I want to create a iPhone static lib to be used by objective-c and C++ users. Since in this special case there will be one and only one lib written in pascal, I though this would make it possible. I saw that my own iOS projects that were based on templates worked by compiling a static libFPC.a that would be then called by Objective-C. So I did create my own static library by gathering all .o files from FPC into a .a file. I wrote a .h header and added the static lib into a Xcode Objective-C project. Ok, it compiles and links correctly. It calls some of the pascal functions correctly. But.. then it later crashes in one of the pascal functions. Note that I have a lib_init() function called from C that is used to initialize the pascal library, and it calls this undocumented fpc function: procedure internal_initializeunits(); external name 'FPC_INITIALIZEUNITS'; This function from what I understood it calls all units initialization sections, right? Still, I'm missing something... the libFPC.a compiled in xcode templates works fine with Objective-C, but also it does not share any memory back or forward. This seems to be some problem with memory allocation, since only calling any function that creates pascal strings or classes seem to crash the app. Any clue? _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Jonas Maebe
to Sergio Flores: Let's start this email by saying that I know perfectly that FPC does not support static…
Sergio Flores wrote 14 lines
So I did create my own static library by gathering all .o files from FPC into a .a file. I wrote a .h header and added the static lib into a Xcode Objective-C project. Ok, it compiles and links correctly. It calls some of the pascal functions correctly. But.. then it later crashes in one of the pascal functions. Note that I have a lib_init() function called from C that is used to initialize the pascal library, and it calls this undocumented fpc function: procedure internal_initializeunits(); external name 'FPC_INITIALIZEUNITS'; This function from what I understood it calls all units initialization sections, right?

On OS X, you must not call that or any other initialisation function manually, because even static libraries can contain an initialisation section on that platform. If you do exactly the same as the iOS Xcode template, then your static library will contain all object files generated for a dynamic library and one of these will already cause the RTL intialisation code to be called.

Jonas

Reply by email