How to build .so with FPC and use it in C program?

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

How to build .so with FPC and use it in C program?

Коньков Евгений
Hello, FPC-Pascal.

How to build .so with FPC and use it in C program on FreeBSD platform?

--
 KES                          mailto:[hidden email]

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to build .so with FPC and use it in C program?

Jonas Maebe-2

On 11 Dec 2008, at 12:31, KES wrote:

> How to build .so with FPC and use it in C program on FreeBSD platform?

FPC cannot build dynamic libraries on FreeBSD at this time (well, at  
least none that work properly with C programs).


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to build .so with FPC and use it in C program?

Graeme Geldenhuys-2
On Thu, Dec 11, 2008 at 1:46 PM, Jonas Maebe <[hidden email]> wrote:
>
>> How to build .so with FPC and use it in C program on FreeBSD platform?
>
> FPC cannot build dynamic libraries on FreeBSD at this time (well, at least
> none that work properly with C programs).

Hi Jonas,

Can this be done for other platforms?  eg: Linux 32/64bit and Windows
32/64bit etc...
I'm mostly interested in 32bit Linux & Windows OS's.


Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to build .so with FPC and use it in C program?

Jonas Maebe-2

On 11 Dec 2008, at 13:14, Graeme Geldenhuys wrote:

> On Thu, Dec 11, 2008 at 1:46 PM, Jonas Maebe <[hidden email]
> > wrote:
>>
>>> How to build .so with FPC and use it in C program on FreeBSD  
>>> platform?
>>
>> FPC cannot build dynamic libraries on FreeBSD at this time (well,  
>> at least
>> none that work properly with C programs).
>
> Can this be done for other platforms?  eg: Linux 32/64bit and Windows
> 32/64bit etc...
> I'm mostly interested in 32bit Linux & Windows OS's.

Only the FreeBSD port is broken in that respect as far as I know.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to build .so with FPC and use it in C program?

Felipe Monteiro de Carvalho
In reply to this post by Graeme Geldenhuys-2
I have managed to convert part of the LCL into a library usable in C:

http://wiki.lazarus.freepascal.org/LCL_Bindings

I don't remember which OS I was using, likely Windows.

And the Apache modules are also libraries, which work and connect to a
C software (apache) =)

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to build .so with FPC and use it in C program?

Graeme Geldenhuys-2
In reply to this post by Jonas Maebe-2
On Thu, Dec 11, 2008 at 2:17 PM, Jonas Maebe <[hidden email]> wrote:
>
> Only the FreeBSD port is broken in that respect as far as I know.

Thanks, that's good to know...


Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to build .so with FPC and use it in C program?

Marco van de Voort
In reply to this post by Jonas Maebe-2
In our previous episode, Jonas Maebe said:
> >> FPC cannot build dynamic libraries on FreeBSD at this time (well,  
> >> at least
> >> none that work properly with C programs).
> >
> > Can this be done for other platforms?  eg: Linux 32/64bit and Windows
> > 32/64bit etc...
> > I'm mostly interested in 32bit Linux & Windows OS's.
>
> Only the FreeBSD port is broken in that respect as far as I know.

I'm not entirely sure how it should work at all. Linux calls the "main" of a
.so, but I never found any docs for that.

At first I waited for the si_* stuff to finish, but that stalled, and
requires several things to be supported in the assembler reader to function
for FreeBSD.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to build .so with FPC and use it in C program?

Jonas Maebe-2

On 11 Dec 2008, at 15:03, Marco van de Voort wrote:

> In our previous episode, Jonas Maebe said:
>>>> FPC cannot build dynamic libraries on FreeBSD at this time (well,
>>>> at least
>>>> none that work properly with C programs).
>>>
>>> Can this be done for other platforms?  eg: Linux 32/64bit and  
>>> Windows
>>> 32/64bit etc...
>>> I'm mostly interested in 32bit Linux & Windows OS's.
>>
>> Only the FreeBSD port is broken in that respect as far as I know.
>
> I'm not entirely sure how it should work at all. Linux calls the  
> "main" of a
> .so, but I never found any docs for that.

Under Linux, you pass the library's init and fini routines to the  
static linker using the "-init" and "-fini" parameters. See compiler/
systems/t_linux.pas.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to build .so with FPC and use it in C program?

Seth Grover
In reply to this post by Коньков Евгений
I do it all the time. It works fine in Linux as of 2.2.2. There was a
problem in 2.2.0 where initialization and finalization code of units
in your uses section wouldn't get called, but they do now.

------- test.lpr -------------------------------
library test;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils;

  function SquareNumber(const input : integer) : integer; cdecl;
  begin
    writeln('libtest.so SquareNumber');
    result := input * input;
  end;

exports
  SquareNumber name 'SquareNumber';

begin
  writeln('libtest.so initialization');

end.
------------------------------------------------

------- ctest.c -------------------------------
#include <stdio.h>

int SquareNumber(const int input);

main()
{
  int input;
  int output;

  input = 4;
  output = SquareNumber(4);

  printf("%i squared is %i\n", input, output);
}
-----------------------------------------------

To build the .so:

fpc -Sgi -CX -Xs -XX -vewnhi -l -Fu. -olibtest.so test.lpr

To build the c program (make sure the .so is in your link path)

gcc -o ctest -ltest ctest.c

Then run it:

libtest.so initialization
libtest.so SquareNumber
4 squared is 16

-SG

========================
Computer over. Virus = very yes.

Seth Grover
sethdgrover[at]gmail[dot]com
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: How to build .so with FPC and use it in C program?

Коньков Евгений
Здравствуйте, Seth.


kes# fpc -Sgi -CX -Xs -XX -vewnhi -l -Fu. -olibtest.so test.lpr
Hint: Start of reading config file /usr/local/etc/fpc.cfg
Hint: End of reading config file /usr/local/etc/fpc.cfg
Free Pascal Compiler version 2.2.0 [2008/11/09] for i386
Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: FreeBSD/ELF for i386
Compiling test.lpr
Linking libtest.so
20 lines compiled, 2.7 sec
2 hint(s) issued
Для продолжения нажмите любую клавишу...
kes# cp libtest.so /usr/lib/
kes# gcc -o ctest -ltest ctest.c
/var/tmp//ccFJez1e.o(.text+0x20): In function `main':
: undefined reference to `SquareNumber'
/usr/lib/libtest.so: undefined reference to `__fpucw'
Для продолжения нажмите любую клавишу...


Вы писали 12 декабря 2008 г., 1:54:51:

SG> I do it all the time. It works fine in Linux as of 2.2.2. There was a
SG> problem in 2.2.0 where initialization and finalization code of units
SG> in your uses section wouldn't get called, but they do now.

SG> ------- test.lpr -------------------------------
SG> library test;

SG> {$mode objfpc}{$H+}

SG> uses
SG>   Classes, SysUtils;

SG>   function SquareNumber(const input : integer) : integer; cdecl;
SG>   begin
SG>     writeln('libtest.so SquareNumber');
SG>     result := input * input;
SG>   end;

SG> exports
SG>   SquareNumber name 'SquareNumber';

SG> begin
SG>   writeln('libtest.so initialization');

SG> end.
SG> ------------------------------------------------

SG> ------- ctest.c -------------------------------
SG> #include <stdio.h>

SG> int SquareNumber(const int input);

SG> main()
SG> {
SG>   int input;
SG>   int output;

SG>   input = 4;
SG>   output = SquareNumber(4);

SG>   printf("%i squared is %i\n", input, output);
SG> }
SG> -----------------------------------------------

SG> To build the .so:

SG> fpc -Sgi -CX -Xs -XX -vewnhi -l -Fu. -olibtest.so test.lpr

SG> To build the c program (make sure the .so is in your link path)

SG> gcc -o ctest -ltest ctest.c

SG> Then run it:

SG> libtest.so initialization
SG> libtest.so SquareNumber
SG> 4 squared is 16

SG> -SG

SG> ========================
SG> Computer over. Virus = very yes.

SG> Seth Grover
SG> sethdgrover[at]gmail[dot]com
SG> _______________________________________________
SG> fpc-pascal maillist  -  [hidden email]
SG> http://lists.freepascal.org/mailman/listinfo/fpc-pascal



--
С уважением,
 KES                          mailto:[hidden email]

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: How to build .so with FPC and use it in C program?

Marc Santhoff
Am Freitag, den 12.12.2008, 13:10 +0200 schrieb KES:

> Здравствуйте, Seth.
>
>
> kes# fpc -Sgi -CX -Xs -XX -vewnhi -l -Fu. -olibtest.so test.lpr
> Hint: Start of reading config file /usr/local/etc/fpc.cfg
> Hint: End of reading config file /usr/local/etc/fpc.cfg
> Free Pascal Compiler version 2.2.0 [2008/11/09] for i386
> Copyright (c) 1993-2007 by Florian Klaempfl
> Target OS: FreeBSD/ELF for i386
> Compiling test.lpr
> Linking libtest.so
> 20 lines compiled, 2.7 sec
> 2 hint(s) issued
> Для продолжения нажмите любую клавишу...
> kes# cp libtest.so /usr/lib/
> kes# gcc -o ctest -ltest ctest.c
> /var/tmp//ccFJez1e.o(.text+0x20): In function `main':
> : undefined reference to `SquareNumber'
> /usr/lib/libtest.so: undefined reference to `__fpucw'
> Для продолжения нажмите любую клавишу...

Let me guess, you're using FreeBSD?

$ sh build.sh
Hint: Start of reading config file /home/marc/.fpc.cfg
Hint: End of reading config file /home/marc/.fpc.cfg
Free Pascal Compiler version 2.0.4 [2007/01/19] for i386
Copyright (c) 1993-2006 by Florian Klaempfl
Target OS: FreeBSD/ELF for i386
Compiling test.lpr
Linking libtest.so
20 Lines compiled, 0.7 sec
/tmp/ccCO3Bkd.o: In function `main':
/tmp/ccCO3Bkd.o(.text+0x13): undefined reference to `SquareNumber'
./libtest.so: undefined reference to `__fpucw'

$ cat build.sh
# !/bin/sh
fpc -Sgi -CX -Xs -XX -vewnhi -l -Fu. -otest.so test.lpr
gcc -o ctest -Wl,-L. -ltest ctest.c

$ uname -mrs
FreeBSD 4.11-STABLE i386

It was mentioned in this thread, it's a known issue for FreeBSD.

Marc

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

>
> Вы писали 12 декабря 2008 г., 1:54:51:
>
> SG> I do it all the time. It works fine in Linux as of 2.2.2. There was a
> SG> problem in 2.2.0 where initialization and finalization code of units
> SG> in your uses section wouldn't get called, but they do now.
>
> SG> ------- test.lpr -------------------------------
> SG> library test;
>
> SG> {$mode objfpc}{$H+}
>
> SG> uses
> SG>   Classes, SysUtils;
>
> SG>   function SquareNumber(const input : integer) : integer; cdecl;
> SG>   begin
> SG>     writeln('libtest.so SquareNumber');
> SG>     result := input * input;
> SG>   end;
>
> SG> exports
> SG>   SquareNumber name 'SquareNumber';
>
> SG> begin
> SG>   writeln('libtest.so initialization');
>
> SG> end.
> SG> ------------------------------------------------
>
> SG> ------- ctest.c -------------------------------
> SG> #include <stdio.h>
>
> SG> int SquareNumber(const int input);
>
> SG> main()
> SG> {
> SG>   int input;
> SG>   int output;
>
> SG>   input = 4;
> SG>   output = SquareNumber(4);
>
> SG>   printf("%i squared is %i\n", input, output);
> SG> }
> SG> -----------------------------------------------
>
> SG> To build the .so:
>
> SG> fpc -Sgi -CX -Xs -XX -vewnhi -l -Fu. -olibtest.so test.lpr
>
> SG> To build the c program (make sure the .so is in your link path)
>
> SG> gcc -o ctest -ltest ctest.c
>
> SG> Then run it:
>
> SG> libtest.so initialization
> SG> libtest.so SquareNumber
> SG> 4 squared is 16
>
> SG> -SG
>
> SG> ========================
> SG> Computer over. Virus = very yes.
>
> SG> Seth Grover
> SG> sethdgrover[at]gmail[dot]com
> SG> _______________________________________________
> SG> fpc-pascal maillist  -  [hidden email]
> SG> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
>
>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re[2]: How to build .so with FPC and use it in C program?

Коньков Евгений
In reply to this post by Jonas Maebe-2
Hello, Jonas.

11 dec 2008 г., 14:17:21: was wrote


JM> On 11 Dec 2008, at 13:14, Graeme Geldenhuys wrote:

>> On Thu, Dec 11, 2008 at 1:46 PM, Jonas Maebe <[hidden email]
>> > wrote:
>>>
>>>> How to build .so with FPC and use it in C program on FreeBSD  
>>>> platform?
>>>
>>> FPC cannot build dynamic libraries on FreeBSD at this time (well,  
>>> at least
>>> none that work properly with C programs).
>>
>> Can this be done for other platforms?  eg: Linux 32/64bit and Windows
>> 32/64bit etc...
>> I'm mostly interested in 32bit Linux & Windows OS's.

JM> Only the FreeBSD port is broken in that respect as far as I know.


Is there plan to fix FreeBSD port?

--
С уважением,
 KES                          mailto:[hidden email]

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: How to build .so with FPC and use it in C program?

Jonas Maebe-2

On 14 Jan 2009, at 20:00, KES <[hidden email]> wrote:

> Hello, Jonas.
>
> 11 dec 2008 г., 14:17:21: was write
>>>>
>>>
>
> JM> Only the FreeBSD port is broken in that respect as far as I know.
>
>
> Is there plan to fix FreeBSD port?

I guess one of the FreeBSD maintainers will fix it at some point. It's  
best to file bug report so it won't be forgotten.


Jonas_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal