[fpc-pascal] Re: How to build .so with FPC and use it in C program?
Seth Grover
sethdgrover at gmail.com
Fri Dec 12 00:54:51 CET 2008
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
More information about the fpc-pascal
mailing list