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

Mailing lists / fpc-pascal / 2008-12
13 messages · 7 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hello, FPC-Pascal.
How to build .so with FPC and use it in C program on FreeBSD platform?
KES wroteHow 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
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/
Only the FreeBSD port is broken in that respect as far as I know.
Jonas
Can this be done for other platforms? eg: Linux 32/64bit and Windows 32/64bit etc... I'm…
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) =)
Only the FreeBSD port is broken in that respect as far as I know. Jonas
Jonas Maebe wroteOnly 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/
Only the FreeBSD port is broken in that respect as far as I know. Jonas
In our previous episode, Jonas Maebe said:
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.
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
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
Здравствуйте, 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
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 i386It was mentioned in this thread, it's a known issue for FreeBSD.
Marc
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
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