We help IT Professionals succeed at work.

Shared objects entry point

finkelg
finkelg asked
on
I need to migrate a C++ dll writen for nt, It has among other things the dllmain entry point function which initialize everything

what is the equivalent of dllmain windows function in unix
shared objects

Any other good tips on porting from NT dll to Sun .so will be appreciated (including urls etc )

Thanks
Comment
Watch Question

CERTIFIED EXPERT

Commented:
you don't need a "dllmain" on UNIX, the runtime loader (ldd, rld) will do it, usually.
Well programmed C++ things are initialized by constructors, or am I wrong?
More details can be found in  man ld.so.1

Author

Commented:
I have a code used on NT and I do not wish to change it.
There is a dllmain where it reads from registry/ini files some data to a global variables.
Can I do the same on unix or should I build a manuall first function that will init the global parameters from the ini/files for use by all other functions in the .so ?
Can that function be called automatically ?
Thanks
CERTIFIED EXPERT

Commented:
you can define a "start"/"entry" symbol with ld, see
   man ld
or
   info ld
Also see the description about "collect2" in
   man g++
or
   info g++

But keep in mind that these are dirty hacks and not portable across platforms 'cause you need different workarounds to get it working.

Global parameters are initialized using either static initializations in your code, or by calling a special init-function in your constructors.
Tweaking with "dllmain" is not recommended in real world  :-)
Commented:
hi finekelq:

if you are porting NT DllMain() to Solaris2.x using Workshop cc/CC, you can use the special pragma tags to run your code before main() executes / after exit() invoked.
they are:

#pragma init(identifier [ , identifier ] ...)

and

#pragma fini (identifier [, identifier]...)


below are copied from Sun AnswerBook2:
------
#pragma init
#pragma init(identifier [ , identifier ] ...)

Use init to mark identifier as an initialization function. Such functions are expected to be of type void, to accept no arguments, and to be called while constructing the memory image of the program at the start of execution. Initializers in a shared object are executed during the operation that brings the shared object into memory, either at program start up or during some dynamic loading operation, such as dlopen(). The only ordering of calls to initialization functions is the order in which they are processed by the link editors, both static and dynamic.

Within a source file, the functions specified in #pragma init are executed after the static constructors in that file. You must declare the identifiers before using them in the pragma.

#pragma fini
#pragma fini (identifier [, identifier]...)

Use fini to mark identifier as a finalization function. Such functions are expected to be of type void, to accept no arguments, and to be called either when a program terminates under program control or when the containing shared object is removed from memory. As with initialization functions, finalization functions are executed in the order processed by the link editor.

In a source file, the functions specified in #pragma fini are executed after the static destructors in that file. You must declare the identifiers before using them in the pragma.
------

may this help plz let me know. thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.