[fpc-pascal] Calling function pointer to main program

Jonas Maebe jonas at freepascal.org
Fri Jan 3 20:29:13 CET 2020

On 03/01/2020 20:19, Ryan Joseph via fpc-pascal wrote:
>> On Jan 3, 2020, at 11:35 AM, Jonas Maebe <jonas at freepascal.org> wrote:
>> Additionally, you will also have to link in an object/library that does
>> define a regular "main" function. And note that this will only work on
>> libc-based targets (afaik only Darwin, Solaris, and AIX at this point).
> So there needs to be a function named "main" that is linked to directly or can it just be in a unit? I tried doing this but still get linker errors (tested on MacOS of course). My example program is below.
> It's possible I think -XM does something it doesn't also so here's is the main function for the SDL/ios bindings. Note how UIApplicationMain is called which then never returns control until the program exists. After that within the iOS event handlers a call is made to SDL_main (see the external definition) which if I understand correctly is set using -XP and this then in turn calls the begin..end block of the main Pascal program. Is that correct?

The OS startup code always calls a C function called "main" with the
default C main function arguments. By default, FPC names the main entry
point symbol of the program "main". -XM simply changes this name.

Your errors are that your main function in your unit
1) is not declared as "cdecl", so it uses the wrong calling convention
2) additionally, is not declared as "public", so the only the default
(Pascal-mangled) symbol is created for it (in combination with cdecl,
this will add a C-mangled alias to it).

So add 'cdecl; public;" to its declaration and definition.


More information about the fpc-pascal mailing list