Sign in Join

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

Mailing lists / fpc-pascal / 2008-12

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

13 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

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

Jonas Maebe
to KES
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

Reply by email

Graeme Geldenhuys
to Jonas Maebe
Jonas Maebe wrote 5 lines
> 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/

Reply by email

Jonas Maebe
to Graeme Geldenhuys
Graeme Geldenhuys wrote 13 lines
On Thu, Dec 11, 2008 at 1:46 PM, Jonas Maebe < > 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

Reply by email

Marco van de Voort
to Jonas Maebe: Only the FreeBSD port is broken in that respect as far as I know. Jonas

In our previous episode, Jonas Maebe said:

Show quoted text 9 lines
>> 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 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.

Reply by email

Jonas Maebe
to Marco van de Voort
Marco van de Voort wrote 15 lines
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 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

Reply by email

Seth Grover

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

Reply by email

KES
to Seth Grover

Здравствуйте, 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 -
SG> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

С уважением, KES

Reply by email

Marc Santhoff
to KES
KES wrote 20 lines
Здравствуйте, 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 - > SG> http://lists.freepascal.org/mailman/listinfo/fpc-pascal > > >

Reply by email

KES
to Jonas Maebe: Only the FreeBSD port is broken in that respect as far as I know. Jonas

Hello, Jonas.

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

Graeme Geldenhuys wrote 13 lines
> On Thu, Dec 11, 2008 at 1:46 PM, Jonas Maebe < > > 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

Reply by email

Jonas Maebe
to KES
KES wrote 10 lines
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

Reply by email