Sign in Join

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

Mailing lists / fpc-pascal / 2010-05

Linking FPC code to a C program

8 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Unknown author

Hi,

is it possible to link FPC compiled units to a C program on Linux? I'm not able to figure it out.
FPC uses some strange way to name symbols and whatever I try, I always end up with the linking errors.
I'm probably doing some trivial error, but I just can't find it.

Simple function:

function aplusb(a,b:longint):longint;cdecl;

gets exported as something like:

text.n_testunit_aplusb$longint$longint$$longint

Any help appreciated. (And I need the same on Windows later, I just didn't get there yet).

VK

Felipe Monteiro de Carvalho
to Unknown author

It's normal name mangling, just like C++ also does. To force a specific name try something like this:

function aplusb(a,b:longint):longint;cdecl name '_aplusb';
Felipe Monteiro de Carvalho
Unknown author
to Felipe Monteiro de Carvalho

Hi a thank you for quick reply, but I've tried that before:

Fatal: Syntax error, ";" expected but "identifier NAME" found

Could it be mode dependant? (I use -Mobjfpc)

V.

Show quoted text 11 lines
It's normal name mangling, just like C++ also does. To force a specific name try something like this: function aplusb(a,b:longint):longint;cdecl name '_aplusb'; -- Felipe Monteiro de Carvalho _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Jonas Maebe
to Unknown author: is it possible to link FPC compiled units to a C program on Linux? I'm not able to figure…
Show quoted text 13 lines
is it possible to link FPC compiled units to a C program on Linux? I'm not able to figure it out. FPC uses some strange way to name symbols and whatever I try, I always end up with the linking errors. I'm probably doing some trivial error, but I just can't find it. Simple function: function aplusb(a,b:longint):longint;cdecl; gets exported as something like: text.n_testunit_aplusb$longint$longint$$longint

Add "export;" at the end of the declaration, then it will also get an unmangled symbol name (but only because it's declared as cdecl). And please do not follow the other advice you got about manually adding an alias, because C name mangling differs from platform to platform and hence the name you'd specify would only work on some platforms.

Jonas

Unknown author
to Felipe Monteiro de Carvalho

YES! Thank you very much. You saved my day (maybe more of them, if I had to rewrite all the pascal code to C) :)

Show quoted text 12 lines
Ah, sorry, that would be for the other direction =) This one should work: function aplusb(a,b:longint):longint;cdecl; public name '_aplusb'; -- Felipe Monteiro de Carvalho _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal