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

Marc Santhoff M.Santhoff at t-online.de
Fri Dec 12 20:50:24 CET 2008


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  -  fpc-pascal at lists.freepascal.org
> SG> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
> 
> 
> 




More information about the fpc-pascal mailing list