Frank Heckenbach
Adriaan van Os wrote:
Waldek Hebisch wrote:
As far as I know, the opposite switch is not available, namely to compile *and link* a unit. This is useful when compiling a unit into a shared library, while still using the specs file instead of a separate ld run (which could be less compatible).
Why do not you link shared library as a program (using `-fgpc-main=' option)? At first glance this should avoid most problems with shared libraries.
At first sight, that looks somewhat strange (conceptually), because a library is a collection of routines, like a unit, not a program. Still, I will experiment with it, thanks for the hint.
I also wonder if you (Waldek) misunderstood this option. It does not tell gp to compile and link. Instead it changes the linker name of the main program. I don't think it's applicable here.
--gpc-main option does not affect linking. But it allows to put pascal program (not a unit) in a library, by eliminating link error due to duplicate main. IIUC gp will link main program, even if other options tell it to make shared library.
It solves less problems that I thougt: my idea was to call fake main function to initialize the shared library. But there are two problems here: 1) main calls _p_finalize. So gpc runtime is unusable after main returns (could be solved by running _p_finalize via atexit) 2) main initializes arguments and environment, which should be done only by a program. Probably we should access environment via libc and initilaze arguments only when present.
Anyway, my attempt at shared libraries (I probably should call `_p_finalize' from ini.c):
gcc -c -fPIC -g ini.c /pom/kompi/gcc/tst44/gpc2-3.4.3/gcc/xgpc \ -B/pom/kompi/gcc/tst44/gpc2-3.4.3/gcc/ \ -shared -fPIC -fautomake -fgpc-main=mylib_main -g mylib.p ini.o -o libmylib.so gcc -g trr.c -L. -lmylib -Wl,-rpath=/pom/kompi/gcc/tst44/gpc2-3.4.3/gcc/ \ -Wl,-rpath=/tmp
Note: to make shared library one needs shared libgpc. My system gpc has only static libgpc, so I used another gpc copy (hence the long paths). Also, since the shared libraries are in non-system directories I need extra magic (`-Wl,-rpath=...' options) to say to the dynamic linker where to find them.
Note2: My goal was to have a dynamic library which is usable from non-Pascal main program. Except for `_p_finalize' problem the library can also be used from Pascal main program (and in principle one should be able to use multiple shared libraries).
-----------------<ini.c>----------------------------
extern void _p__rts_Init_init(void); extern void mylib_init(void);
void ini(void) __attribute__((constructor)) /* __attribute__((section (".init"))) */ ;
void ini(void) { _p__rts_Init_init(); mylib_init(); } -------------------------------------------------------
-------------------------<mylib.p>--------------------- program mylib; attribute (name = 'mylib'); uses u1; begin end . -------------------------------------------------------
-------------------------<u1.p>------------------------ unit u1; interface procedure p; attribute(name = 'mylib_p'); implementation procedure p; begin end; initialization begin writeln('u1 init') end; end . -------------------------------------------