Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2001-05

LINKING a pp library from a MAIN.C produces ERROR when calling WRITELN? (Moderator note: downsized...)

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Javier Ros Ganuza

I'm trying to make available a pascal library to the C people.

So my first step was to build a minimal example.

I've defined and exported a procedure in a library pascal file and I've made a main_c.c and a main_pascal.pp files that uses such procedure from such library.

Using gdb I follow the flux of execution, and calling seems to be correct in both cases (main_c or main_pascal).
But in the C case, when
(from inside the pascal library) the funtion writeln is called the aplication exits with error code 0147.

If the debuger is not used, the shell do not report any error at all, although the aplication exits abnormally.

I supouse the problem is that the C executable should be linked to some other libraries, or perhaps it is related to the pascal initialization of the io system or something related to standard io streams.

Could some body point me to the solution. In case the soluton comes from having to add some pascal libraries, could you specify which ones?

May is a trivial thing and I'm an stupid, but if it is a library isue it would help having a comment in the docs around the libraries that
should be linked with,...

NOTE 1:
The compiler I'm using is:

Free Pascal Compiler version 1.0.4 [2000/12/18] for i386 Copyright (c) 1993-2000 by Florian Klaempfl

NOTE 2: I've attached a tgz file with a directory with the files pertaining to the above mentioned example, for if it helps.

Thanks in advance.

Javier Ros Ganuza. Dpto. Ingeniería Mecánica./Ingeniaritza Mekanika Saila. Univ. Pública de Navarra./Nafarroako Unibertsitate Publikoa. Campus de Arrosadia s/n. 31006 Iruñea. Tel. 34 948169307 Fax. 34 948169099

Reply by email

Unknown author
to Javier Ros Ganuza
Javier Ros Ganuza wrote 22 lines
I'm trying to make available a pascal library to the C people. So my first step was to build a minimal example. I've defined and exported a procedure in a library pascal file and I've made a main_c.c and a main_pascal.pp files that uses such procedure from such library. Using gdb I follow the flux of execution, and calling seems to be correct in both cases (main_c or main_pascal). But in the C case, when (from inside the pascal library) the funtion writeln is called the aplication exits with error code 0147. If the debuger is not used, the shell do not report any error at all, although the aplication exits abnormally. I supouse the problem is that the C executable should be linked to some other libraries, or perhaps it is related to the pascal initialization of the io system or something related to standard io streams.

The problem is that the system unit (nor any other unit) is not initialized on Linux or DOS; you should manually initialize all units.

The best you can do is call FPC_INITIALIZEUNITS, and this will initialize all units for you.

We are working to solve this problem.

Michael.

Reply by email

Javier Ros Ganuza
to Unknown author

Michael, thanks for your quick answer:

I'm now trying to elucidate how to call such a function, and the only reference I've found to it has been something like emitinsertcall('FPC_INITIALIZEUNITS'); within the compiler source, but I don't know how to call such function from a normal program. Could you be more specific or point me to some place within the docs or to some example?. I haven't benn able to find more information...

I supouse that I have to make a especial mylibrary initialization procedure, to be called from the main C program, though library, unlike units, do not have initialization and termination codes.

Could be a more elegant trick to put a specific intialization unit in the uses clause of the library ?

Show quoted text 14 lines
The problem is that the system unit (nor any other unit) is not initialized on Linux or DOS; you should manually initialize all units. The best you can do is call FPC_INITIALIZEUNITS, and this will initialize all units for you. We are working to solve this problem. Michael. _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel
Javier Ros Ganuza. Dpto. Ingeniería Mecánica./Ingeniaritza Mekanika Saila. Univ. Pública de Navarra./Nafarroako Unibertsitate Publikoa. Campus de Arrosadia s/n. 31006 Iruñea. Tel. 34 948169307 Fax. 34 948169099

Reply by email

Michael Van Canneyt
to Javier Ros Ganuza
Javier Ros Ganuza wrote 8 lines
Michael, thanks for your quick answer: I'm now trying to elucidate how to call such a function, and the only reference I've found to it has been something like emitinsertcall('FPC_INITIALIZEUNITS'); within the compiler source, but I don't know how to call such function from a normal program. Could you be more specific or point me to some place within the docs or to some example?. I haven't benn able to find more information...

You must simply declare it as an external procedure, and call it. This is explained in the docs.

In a C program you should declare something like

extern void FPC_INITIALIZEUNITS (void);

and then just

main () {

  FPC_INITIALIZEUNITS();

}

in a pascal program, do a

procedure FPC_INITIALIZEUNITS; external;
begin
  FPC_INITIALIZEUNITS;
Show quoted text 7 lines
I supouse that I have to make a especial mylibrary initialization procedure, to be called from the main C program, though library, unlike units, do not have initialization and termination codes. Could be a more elegant trick to put a specific intialization unit in the uses clause of the library ?

No that will not help.

Michael.

Reply by email

Javier Ros Ganuza
to Michael Van Canneyt

Michael, it worked!!!!.

Do you want me to add some footnote comment on the

programer's manual->programing libraries->Using a library from a C program ??

Many thanks

Michael Van Canneyt wrote 49 lines
On Wed, 23 May 2001, Javier Ros Ganuza wrote: > Michael, thanks for your quick answer: > > I'm now trying to elucidate how to call such a function, and the only > reference I've found to it has been something like > emitinsertcall('FPC_INITIALIZEUNITS'); within the compiler source, but I > don't know how to call such function from a normal program. Could you be > more specific or point me to some place within the docs or to some > example?. I haven't benn able to find more information... You must simply declare it as an external procedure, and call it. This is explained in the docs. In a C program you should declare something like extern void FPC_INITIALIZEUNITS (void); and then just main () { FPC_INITIALIZEUNITS(); } in a pascal program, do a procedure FPC_INITIALIZEUNITS; external; begin FPC_INITIALIZEUNITS; > > I supouse that I have to make a especial mylibrary initialization > procedure, to be called from the main C program, though library, unlike > units, do not have initialization and termination codes. > > Could be a more elegant trick to put a specific intialization unit in > the uses clause of the library ? No that will not help. Michael. _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel
Javier Ros Ganuza. Dpto. Ingeniería Mecánica./Ingeniaritza Mekanika Saila. Univ. Pública de Navarra./Nafarroako Unibertsitate Publikoa. Campus de Arrosadia s/n. 31006 Iruñea. Tel. 34 948169307 Fax. 34 948169099

Reply by email

Peter Vreman
to Javier Ros Ganuza
you wrote 6 lines
Michael, it worked!!!!. Do you want me to add some footnote comment on the programer's manual->programing libraries->Using a library from a C program ??

We are working on a nicer solution to use a .ctors and .dtors entry for the initialization of the units. But this will also require that FPC can parse those sections when libc is not used.

Btw. With you current solution only the units are initialized. The finalization and exitproc routines are not called when the program is stopped.

Peter

Reply by email