Alex Rivera | Logout

How to initialize a shared library on Linux

Asked 2009-11-05T15:08:51.253
11

I am developing a shared library using C++ under Linux, and I would like this library to use log4cxx for logging purposes. However, I'm not sure how to set this up. For log4cxx to work, I need to create a logger object. How can I make sure this object is created when my library is loaded?

I suspect that it will be easiest to create the logger object as a global variable and then use it from any of the source files of my library, declaring it as extern in the headers. But how can I have the logger created automatically once an application connects to the library?

I know that in DLLs for Windows, there's a thing as REASON_FOR_CALL == PROCESS_ATTACH; is there a similar thing under Linux?

Edit
Report

1 Answer

4

Using a global (or a local-static wrapped up in a function) is nice... but then you enter the land of static initialization fiasco (and the actual destruction is not pretty either).

I would recommend to have a look at the Singleton implementation of Loki.

There are various lifetime policies, one of which is Phoenix and will help you avoid this fiasco.

When you are at it, read Modern C++ Design which explains the problems encountered by the Singleton in depth as well as the uses for the various policies.

answered 2009-11-05T16:18:27.353

Your Answer