7-Jul-00 14:36 you wrote:
Hello all,
Is someone could help me about using C routines in pascal? I think I am close to the right solution but I can't manage to get it!...
You really close to right solution.
So I will try to summurize all I made until now... At first, I have downloaded randlibc-1.3.tar.gz. After having unpacked this file, a randlib.c directory was created and it contains: HOWTOGET* README* doc/ src/ test/
In src, I have compiled com.c, linpack.c and randlib.c and put them in a library lirandlib.a Then, I have move librandlib.a into /usr/local/lib and randlib.h in /usr/local/include.
Great. Just randlib.h is not needed for Pascal at all: you must define crrect prototypes in Pascal anyway.
Actually, I would like to use a fonction named "genbet" in the randlib.c. For that, I have made a little program beta.pas:
unit beta; interface {$L /usr/local/include/randlib.h} function genbet(x,y:shortreal):shortreal;C; implementation end.
Alost correct. Just not {$L /usr/local/include/randlib.h} but {$L randlib} ...
and test.pas: uses beta; begin writeln(genbet(100,150)); end.
When I compile the first one, the following message appears: Undefined first referenced symbol in file main /usr/local/lib/gcc-lib/sparc-sun-solaris2.7/ 2.95.2/crt1.o ld: fatal: Symbol referencing errors. No output written to beta collect2: ld returned 1 exit status
It's normal. Default last step for GPC (just like GCC) is to LINK program. You can not do THAT with unit. Just use `gpc -c' to get rid of error.
And for the second one: test.p:4: object file `/usr/local/include/randlib.o' not found test.p:4: object file `/usr/local/include/randlib.o' not found
Yeah, it's since you are written {$L /usr/local/include/randlib.h}. GPC "thinks" it's C source and thus tries to find randlinb.o and NOT tries to use existing librandlib.a ...
"extern float genbet(float aa,float bb)" is in randlib.h and the program is written in randlib.c.
You did not know ANYTHING about C, right ? .h file is more or less like interface part of Pascal's unit (or module). It's just declare functions - there are NO implemetation. .c part is more or less like implemetation part of Pascal's unit (or module). Ok, it's not part of C language definition - it's just convention, but anyway. Thus you NEVER need .h file for Pascal program: Pascal can not use .h at all. You must write substitute interface module (and your beta is more or less correct substitute module). But then Pascal need to know where to find implementation part (this part Pascal can use). For this you need $L directive. Of course it's useless to point it on .h file - it's interface, NOT implementation. You should point it to .c file, .o file or (better of all) to .a file. When you pointing to .a file you should omit path information, lib- prefix for library and .a suffix (it'a also old C linker convention). Thus your /usr/local/lib/librandlib.a will be reffered as {$L randlib} (yu can specify -L/usr/local/lib as command line parameters but it should not be needed - usually /usr/local/lib is in linker patch anyway). Hope it helps.
I really need your help,
Nathalie