Porting Dlls to Linux

Porting Dlls to Linux

Post by D. Stimit » Thu, 01 Mar 2001 05:51:54




> Hi,

> I am porting a MSVC application to RH6.2 Linux. There are
> several DLL's used in the application. Can someone give me
> some guideline for dealing with DLLs in Linux?

> I think one way is to just use static link in Linux. However,
> I am kind of worried about the name conflication in different
> DLLs. There are some initialization work in the DllMain of
> some Dlls. I think one way to circumvent of the problem is
> to call the DllMains of each Dll library the first thing in the
> main program. However, it seems quite awkward. Is there any better
> way to deal with the initialization problem?

> Thanks in advance.

> Manchun Yao

You might find something of interest in an old Linux Journal article. Go
to:
http://www2.linuxjournal.com/cgi-bin/frames.pl/index.html

From there, search for "self-registering". There is some information
about libraries and C++ namespace issues.

 
 
 

Porting Dlls to Linux

Post by Nate Eldredg » Thu, 01 Mar 2001 06:22:13



> Hi,

> I am porting a MSVC application to RH6.2 Linux. There are
> several DLL's used in the application. Can someone give me
> some guideline for dealing with DLLs in Linux?

> I think one way is to just use static link in Linux. However,
> I am kind of worried about the name conflication in different
> DLLs. There are some initialization work in the DllMain of
> some Dlls. I think one way to circumvent of the problem is
> to call the DllMains of each Dll library the first thing in the
> main program. However, it seems quite awkward. Is there any better
> way to deal with the initialization problem?

Shared libraries can contain constructors.  So you could write

static void setup(void) __attribute__((constructor));

static void setup(void)
{
        do_stuff();

Quote:}

and then `setup' will be called before `main'.

--

Nate Eldredge


 
 
 

Porting Dlls to Linux

Post by laccat » Thu, 01 Mar 2001 20:43:43




> > Hi,

> > I am porting a MSVC application to RH6.2 Linux. There are
> > several DLL's used in the application. Can someone give me
> > some guideline for dealing with DLLs in Linux?

> > I think one way is to just use static link in Linux. However,
> > I am kind of worried about the name conflication in different
> > DLLs. There are some initialization work in the DllMain of
> > some Dlls. I think one way to circumvent of the problem is
> > to call the DllMains of each Dll library the first thing in the
> > main program. However, it seems quite awkward. Is there any better
> > way to deal with the initialization problem?

> Shared libraries can contain constructors.  So you could write

> static void setup(void) __attribute__((constructor));

> static void setup(void)
> {
>         do_stuff();
> }

> and then `setup' will be called before `main'.

> --

> Nate Eldredge


Is there also __attribute__((destructor)) to do the cleanup ??
 
 
 

Porting Dlls to Linux

Post by laccat » Thu, 01 Mar 2001 21:47:52





> > > Hi,

> > > I am porting a MSVC application to RH6.2 Linux. There are
> > > several DLL's used in the application. Can someone give me
> > > some guideline for dealing with DLLs in Linux?

> > > I think one way is to just use static link in Linux. However,
> > > I am kind of worried about the name conflication in different
> > > DLLs. There are some initialization work in the DllMain of
> > > some Dlls. I think one way to circumvent of the problem is
> > > to call the DllMains of each Dll library the first thing in the
> > > main program. However, it seems quite awkward. Is there any better
> > > way to deal with the initialization problem?

> > Shared libraries can contain constructors.  So you could write

> > static void setup(void) __attribute__((constructor));

> > static void setup(void)
> > {
> >         do_stuff();
> > }

> > and then `setup' will be called before `main'.

> > --

> > Nate Eldredge

> Is there also __attribute__((destructor)) to do the cleanup ??

I tried this (constructor and destructor).  g++ didn't complain, but
nothing happened.
I am developing an application which must run under Linux and NT.  The
following code produces the expected output under NT, but nothing under
Linux.  Any help appreciated.

#ifdef LINUX
  void libsetup(void) __attribute((constructor))__;
  void libterm(void) __attribute((destructor))__;
#endif

void libsetup(void)
{
  cout << "\nHello from libsetup()\n" << endl;

Quote:}

void libterm(void)
 {
  cout << "\nHello from libterm()\n" << endl;
 }

#ifdef WIN32
  BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID impLoad )
  {

   switch (reason)
   {
    case DLL_PROCESS_ATTACH:
    {
     libsetup();
     break;
    }

  case DLL_PROCESS_DETACH:
    {
     libterm();
     break;
    }

   default: {  break; }

   } //end case

 return TRUE;

Quote:}

#endif
 
 
 

Porting Dlls to Linux

Post by Arthur H. Gol » Fri, 02 Mar 2001 06:47:52






> > > > Hi,

> > > > I am porting a MSVC application to RH6.2 Linux. There are
> > > > several DLL's used in the application. Can someone give me
> > > > some guideline for dealing with DLLs in Linux?

> > > > I think one way is to just use static link in Linux. However,
> > > > I am kind of worried about the name conflication in different
> > > > DLLs. There are some initialization work in the DllMain of
> > > > some Dlls. I think one way to circumvent of the problem is
> > > > to call the DllMains of each Dll library the first thing in the
> > > > main program. However, it seems quite awkward. Is there any better
> > > > way to deal with the initialization problem?

> > > Shared libraries can contain constructors.  So you could write

> > > static void setup(void) __attribute__((constructor));

> > > static void setup(void)
> > > {
> > >         do_stuff();
> > > }

> > > and then `setup' will be called before `main'.

> > > --

> > > Nate Eldredge

> > Is there also __attribute__((destructor)) to do the cleanup ??

> I tried this (constructor and destructor).  g++ didn't complain, but
> nothing happened.
> I am developing an application which must run under Linux and NT.  The
> following code produces the expected output under NT, but nothing under
> Linux.  Any help appreciated.

> #ifdef LINUX

ITYM: #ifdef linux
or    #ifdef __linux__
or    #ifdef __linux
Quote:>   void libsetup(void) __attribute((constructor))__;
>   void libterm(void) __attribute((destructor))__;

ITYM:
    void libsetup(void) __attribute__((constructor));
    void libterm(void) __attribute__((destructor));

- Show quoted text -

Quote:> #endif

> void libsetup(void)
> {
>   cout << "\nHello from libsetup()\n" << endl;
> }

> void libterm(void)
>  {
>   cout << "\nHello from libterm()\n" << endl;
>  }

> #ifdef WIN32
>   BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID impLoad )
>   {

>    switch (reason)
>    {
>     case DLL_PROCESS_ATTACH:
>     {
>      libsetup();
>      break;
>     }

>   case DLL_PROCESS_DETACH:
>     {
>      libterm();
>      break;
>     }

>    default: {  break; }

>    } //end case

>  return TRUE;

> }

> #endif

What was happening is that the preprocessor variable `LINUX'
was _not_ defined (see above for the cpp variables that gcc
_does_ define) -- and those lines were therefore omitted (as
evidenced by the fact that you didn't get a syntax error
(also see above).

HTH,
--ag
--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account
for more info)

--
Verbing weirds language.

 
 
 

Porting Dlls to Linux

Post by Ulrich Weiga » Sun, 04 Mar 2001 06:05:30



>I tried this (constructor and destructor).  g++ didn't complain, but
>nothing happened.

I've noticed that global constructors and destructors don't
work with the C++ frontend, only with the C frontend (at least
this is the case with certain versions of g++).

To circumvent this, you have two options:

 - Place the global constructors / destructors in a separate C
   file and compile that file with gcc, not g++.

 - Use C++ standard global constructors / destructors instead of
   the GNU C extensions (i.e. define a global object of a class
   containing the constructor/destructor).

Quote:>#ifdef LINUX
>  void libsetup(void) __attribute((constructor))__;
>  void libterm(void) __attribute((destructor))__;
>#endif

Is this actual code?  I guess you never define LINUX (by default
gcc automatically defines only __linux__, __linux, and maybe linux,
but not LINUX), because otherwise you'd get a syntax error;
the correct syntax is __attribute__((constructor)) ...

--
  Dr. Ulrich Weigand

 
 
 

Porting Dlls to Linux

Post by laccat » Wed, 07 Mar 2001 06:14:38




> >I tried this (constructor and destructor).  g++ didn't complain, but
> >nothing happened.

> I've noticed that global constructors and destructors don't
> work with the C++ frontend, only with the C frontend (at least
> this is the case with certain versions of g++).

> To circumvent this, you have two options:

>  - Place the global constructors / destructors in a separate C
>    file and compile that file with gcc, not g++.

>  - Use C++ standard global constructors / destructors instead of
>    the GNU C extensions (i.e. define a global object of a class
>    containing the constructor/destructor).

> >#ifdef LINUX
> >  void libsetup(void) __attribute((constructor))__;
> >  void libterm(void) __attribute((destructor))__;
> >#endif

> Is this actual code?  I guess you never define LINUX (by default
> gcc automatically defines only __linux__, __linux, and maybe linux,
> but not LINUX), because otherwise you'd get a syntax error;
> the correct syntax is __attribute__((constructor)) ...

> --
>   Dr. Ulrich Weigand


No, it's a typo.  The actual code was correct (the syntax at least).  I
just can't get copy/paste to work between Netscape and kwrite, so end up
(mis)-typing the code.

I've gone for the C++ global constructor.  Thanks.