Sign in Join

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

Mailing lists / fpc-pascal / 2010-05

Linking C object against "Pascal runtime"

4 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,

I have a C file which I want to link statically into my FreePascal application. The C code is mostly self-contained, except for a few simple calls to the C runtime, one of them being "_strlen".

Now "_strlen" is a simple enough function to rewrite it in Pascal, and so I did. However, FreePascal seems unable to recognize my strlen function in Pascal. It keeps reporting "Error: Undefined symbol: _strlen". My code looks like this, and "_strlen" is clearly present:

{$link cfile.o}
function _strlen(s: pansichar): integer; cdecl;
begin
  Result := 0;
  while s^  #0 do
  begin
    Inc(Result); Inc(s);
  end;
end;

Is it not possible to link C object files against an internal "Pascal runtime"? Or if it is, how do I make it happen?

Any solutions would be greatly appreciated!

Moichos

Felipe Monteiro de Carvalho
to Unknown author

FPC automatically does name mangling in your procedures, I guess your procedure really looks currently like:

function _strlen(s: pansichar): integer; cdecl name 'MYUNIT$_STRLEN';

Or even greater, with parameter type info and stuff. I would recommend that you declare it as:

function _strlen(s: pansichar): integer; cdecl name '_strlen';
Felipe Monteiro de Carvalho
Jonas Maebe
to Unknown author: I have a C file which I want to link statically into my FreePascal application. The C…
I have a C file which I want to link statically into my FreePascal application. The C code is mostly self-contained, except for a few simple calls to the C runtime, one of them being "_strlen". Now "_strlen" is a simple enough function to rewrite it in Pascal, and so I did. However, FreePascal seems unable to recognize my strlen function in Pascal. It keeps reporting "Error: Undefined symbol: _strlen". My code looks like this, and "_strlen" is clearly present:

C name mangling is not the same on all systems. On several systems, if you declare a function called "strlen" in C, the assembler symbol will actually be called "_strlen". The C compiler automatically adds the "_" prefix if necessary, and so does FPC when you use the "cdecl" calling convention. So change the "_strlen" into "strlen" and try again.

Jonas