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

KES kes-kes at yandex.ru
Fri Dec 12 12:10:23 CET 2008


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



-- 
С уважением,
 KES                          mailto:kes-kes at yandex.ru




More information about the fpc-pascal mailing list