Sign in Join

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

Mailing lists / fpc-pascal / 2007-06

library initialization and deinitialization

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Seth Grover

I've got a shared library (.so) I wrote in C, with initialization and finalization code declared by using the GCC attributes constructor and destructor. These init and fini functions simply print out "start" and "stop" to stdout. The .so exports one function, which prints out "Hello".

I've got a test program (also written in C) which calls the .so's function. What I see on stdout is

start
Hello
stop

Just like you'd expect.

I wrote a Free Pascal test program which also uses the .so. However, what I see is this:

start
Hello

So for some reason, the fini never seems to get called! I thought this was strange, so I wrote a similar library in free pascal:

==============================================
library subs;
procedure SayHello (); cdecl; export;
begin
  writeln('Hello ');
end;
exports
  SayHello;
initialization
  writeln('start');
finalization
  writeln('done');
end.
==============================================

Again, I ran a c test program against it and a fpc test program against it. The C program's output was:

start
Hello
stop

The freepascal program's output was:

Hello

What's going on here?

With humble thanks,

Seth Grover

Seth Grover sethdgrover[at]gmail[dot]com http://grovers.us/seth Who is John Galt?

Reply by email

L
Show quoted text 20 lines
I've got a shared library (.so) I wrote in C, with initialization and finalization code declared by using the GCC attributes constructor and destructor. These init and fini functions simply print out "start" and "stop" to stdout. The .so exports one function, which prints out "Hello". I've got a test program (also written in C) which calls the .so's function. What I see on stdout is start Hello stop Just like you'd expect. I wrote a Free Pascal test program which also uses the .so. However, what I see is this: start Hello

Haven't closely studied your problem, but probably related to this: http://www.freepascal.org/mantis/view.php?id=6822
http://www.freepascal.org/mantis/view.php?id=7838

Regards,
Lars

Reply by email