Kevan Hashemi wrote:
Dear GPC Users,
I have pascal code that reads and writes from standard io. I would like to compile it into a library that someone who has only GCC can link to.
With the help of the GPC demo programs, I have arrived at the solution I give below. What is the standard way of solving this problem?
Yours, Kevan Hashemi
My Solution
We arrange our pascal code as a program (as in gcc_c_pas.pas demo):
program example; procedure pascal_procedure; attribute (name='Pascal_Procedure'); begin WriteLn('Here we are in the Pascal Procedure.'); Flush(Output) end; begin end.
We compile this program as a dynamic library:
gpc -o example.o -dynamiclib example.pas --gpc-main=dummy
We can look inside with UNIX command "otool -T -v example.o" and see the _p_initialize routine is there for us to link to.
I don't know `otool', but you don't really need it. `_p_initialize' is part of the RTS, so it's always there.
In our C program we have:
extern int _p_initialize (int argc, char **argv, char **envp);
Or you `#include <gpc-in-c.h>' which contains this declaration (and will be adjusted if it ever changed, which is unlikely, though).
and in the main loop or init routine we put:
_p_initialize (0,NULL,NULL);
If you want no command-line arguments and environment variables in Pascal (which are used in turn by other routines, e.g. ParamStr (0) for error reporting, which will then also fail). Otherwise you'd call `_p_initialize (argc, argv, envp);' with the respective C main parameters.
You should also call `init_pascal_main_program ();' (otherwise there may be subtle problems such as global string variables not working).
And at the end call `_p_finalize ();'. Otherweise some cleanup may be missing, e.g. if files buffer output (not now, but perhaps in the future), you won't see any output at all.
Frank