Recent

Author Topic: Calling C function in .o file from lazarus.  (Read 748 times)

BSaidus

  • Hero Member
  • *****
  • Posts: 545
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Calling C function in .o file from lazarus.
« on: November 28, 2020, 01:08:56 pm »
Hello, I have written a little C program composed in 2 c files
One containe
Code: [Select]
main function and the second one containe my function called "CopyFile".
I compiled a project and it work fine.
Then I decided to include the object file in my lazarus project using this code
Code: Pascal  [Select][+][-]
  1. {$L copyfuncs.o}
  2. function fBinFileCopyC(const infile: PChar;
  3.   const outfile: PChar; fp: TfCopyCallback ): Integer; cdecl; external;    
  4.  
.

After compiling I have these error in compile time :
Code: Pascal  [Select][+][-]
  1. Compile Project, Target: testcopc.exe: Exit code 1, Errors: 8
  2. testcopc.lpr(21,1) Error: Undefined symbol: _fopen
  3. testcopc.lpr(21,1) Error: Undefined symbol: _fseek
  4. testcopc.lpr(21,1) Error: Undefined symbol: _fread
  5. testcopc.lpr(21,1) Error: Undefined symbol: _fwrite
  6. testcopc.lpr(21,1) Error: Undefined symbol: _fclose
  7. testcopc.lpr(21,1) Error: Undefined symbol: _ftell
  8. testcopc.lpr(21,1) Error: Undefined symbol: _malloc
  9. testcopc.lpr(21,1) Error: Undefined symbol: _free
  10.  

C function code is
Code: C  [Select][+][-]
  1. int fBinFileCopyC(const char *infile, const char *outfile, fCopyCallback fp ) {
  2.   char *aBuff;
  3.   int iRead,
  4.     bSize,
  5.     ifsize = 0, /* size of the file */
  6.     iftotread = 0;
  7.   FILE *fin  = NULL,
  8.        *fout = NULL;
  9.   /* */
  10.   bSize = 64*1024*1024;
  11.   aBuff = NULL;
  12.   aBuff = (char*)malloc( bSize*sizeof(char) );
  13.   if ( aBuff == NULL) { return -10; }
  14.   if ( (fin  = fopen(infile,  "rb")) == NULL ) {  return -11; }
  15.   if ( (fout = fopen(outfile, "wb")) == NULL ) {  return -12; }
  16.   /* get size of file */
  17.   /*fsetpos(fin, )*/
  18.   if ( fseek(fin, 0, SEEK_END) == 0 ) {
  19.     ifsize = ftell(fin);
  20.   }
  21.   fseek(fin, 0, SEEK_SET);
  22.   /*--------------------------------*/
  23.   while (1) {
  24.     iRead = fread( aBuff, sizeof(char), bSize*sizeof(char), fin );
  25.     if (iRead != 0) {
  26.       fwrite( aBuff, sizeof(char), iRead, fout);
  27.       iftotread += iRead;
  28.       /* put backup fonction here */
  29.       if (fp) { fp( infile, outfile, ifsize, iftotread, iRead ); }
  30.     } else {  break;  }
  31.   }
  32.   free(aBuff);
  33.   fclose(fin);
  34.   fclose(fout);
  35.   return 0;
  36. }
  37.  

So any one have experimented this issue,
Thanks for help.
« Last Edit: November 28, 2020, 01:10:42 pm by BSaidus »
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Peter H

  • Sr. Member
  • ****
  • Posts: 272
Re: Calling C function in .o file from lazarus.
« Reply #1 on: November 28, 2020, 01:36:20 pm »
I think these undefined symbols are from C runtime library, so the C runtime library must be linked.
Not that I have any experience doing this with pascal, I am unsure if the program will work.

BSaidus

  • Hero Member
  • *****
  • Posts: 545
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Calling C function in .o file from lazarus.
« Reply #2 on: November 28, 2020, 02:31:07 pm »
@Piter H
The C program work fine and yes the symbols are from msvcrt C library .
Bjr how to compile a C file into object one without need to external libs
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

BlueIcaro

  • Hero Member
  • *****
  • Posts: 793
    • Blog personal
Re: Calling C function in .o file from lazarus.
« Reply #3 on: November 28, 2020, 03:01:51 pm »
Hi, I don't have any experince, but I found this document, that may can help you.

https://github.com/williamhunter/pascal-bindings-for-c/blob/master/docs/Creating%20Pascal%20bindings%20for%20C%20(v1.0).pdf

/BlueIcaro

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Calling C function in .o file from lazarus.
« Reply #4 on: November 28, 2020, 03:41:06 pm »
@Piter H
The C program work fine and yes the symbols are from msvcrt C library .
Bjr how to compile a C file into object one without need to external libs

You either need to provide those functions yourself or you need to link against the C library. That are the only two choices you have in this case.

Or implement your code in Pascal.

Peter H

  • Sr. Member
  • ****
  • Posts: 272
Re: Calling C function in .o file from lazarus.
« Reply #5 on: November 28, 2020, 03:52:46 pm »
I think the C RTL will use its own memory allocator, so there are two heaps and this could give problems.

BSaidus

  • Hero Member
  • *****
  • Posts: 545
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Calling C function in .o file from lazarus.
« Reply #6 on: November 28, 2020, 05:07:07 pm »
Hi, I don't have any experince, but I found this document, that may can help you.

https://github.com/williamhunter/pascal-bindings-for-c/blob/master/docs/Creating%20Pascal%20bindings%20for%20C%20(v1.0).pdf

/BlueIcaro

Thank you for infos, it help me a lot.
Just do link with libmsvcrt from mingw distro.

Code: [Select]
{$link copy.o}
{$linklib libmsvcrt.a}

It do the work
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

 

TinyPortal © 2005-2018