Dynamic libraries on AIX using gcc compiler

Dynamic libraries on AIX using gcc compiler

Post by Petr Sedlac » Sat, 22 Sep 2001 16:22:25



I have problems creating dynamic library on AIX 4.3.0.1.
I am using gcc compiler (v. 2.95.2) and the library
is linked with ld. I am using dlopen(), dlsym() and dlclose()
to link to the library from the main program. The library
is linked OK, all the symbols are visible.
Problem is the following:
I am using global variables and I need to initialize
them using static function or a constructor, when the
library is loaded (before first function is called),
but if I use a function, for example:

static int foo() { return(1); }
static int x = foo();

the variable x is not initialized and the function
foo() is not called. When I assign the value directly

static int x = 1;

the variable is initialized correctly.
I need to do this using function, because I need
to execute some functions after the library is loaded.
I have tried to compile the same code under
Linux and everything works fine.

Can anybody help me?

Thanks Petr Sedlacek

 
 
 

Dynamic libraries on AIX using gcc compiler

Post by Michael Hode » Tue, 25 Sep 2001 14:09:38


Hello

Have you tried  the functions Init and Fini? They will be called
during loading and unloading of the lib.
extern "C" is only necessary if you use C++.

========================================
extern "C" {

Int Init()
{
    // Initialization goes here
    return( 1 );

Quote:}

Int Fini()
{
    // freeing memory or so
    return( 1 );

Quote:}
}  // extern C

========================================

Regards
Michael Hodel

Petr Sedlacek schrieb:

Quote:> I have problems creating dynamic library on AIX 4.3.0.1.
> I am using gcc compiler (v. 2.95.2) and the library
> is linked with ld. I am using dlopen(), dlsym() and dlclose()
> to link to the library from the main program. The library
> is linked OK, all the symbols are visible.
> Problem is the following:
> I am using global variables and I need to initialize
> them using static function or a constructor, when the
> library is loaded (before first function is called),
> but if I use a function, for example:

> static int foo() { return(1); }
> static int x = foo();

> the variable x is not initialized and the function
> foo() is not called. When I assign the value directly

> static int x = 1;

> the variable is initialized correctly.
> I need to do this using function, because I need
> to execute some functions after the library is loaded.
> I have tried to compile the same code under
> Linux and everything works fine.

> Can anybody help me?

> Thanks Petr Sedlacek


 
 
 

Dynamic libraries on AIX using gcc compiler

Post by Gary R. Hoo » Tue, 25 Sep 2001 23:15:12



> I have problems creating dynamic library on AIX 4.3.0.1.

Time to upgrade the system.  AIX is at 4.3.3.x; that level
is >3 years old.  At the very least, bos.rte.bind_cmds
should be updated to at least 4.3.0.2 if you're using
gcc.

Quote:> I am using gcc compiler (v. 2.95.2) and the library
> is linked with ld. I am using dlopen(), dlsym() and dlclose()
> to link to the library from the main program. The library
> is linked OK, all the symbols are visible.
> Problem is the following:
> I am using global variables and I need to initialize
> them using static function or a constructor, when the
> library is loaded (before first function is called),
> but if I use a function, for example:

> static int foo() { return(1); }
> static int x = foo();

The C language itself does not support this concept outside
the context of a function.  You've moved into implementation-
specific features.

If it works on Linux, then it's a gcc-specific construct on
linux.  Perhaps gcc is automagically creating the required
_init() function.

On AIX (and likely other systems) one uses a init function
that is to be executed at module load or exec time.  This
function would then execute whatever tasks are required
to initialize the module data.  For AIX, look into the
-binitfini ld option in the AIX pubs.

In C++, a static constructor can be used to accomplish
this in some fashion, I think.

--
Gary R. Hook / AIX PartnerWorld for Developers / These opinions are MINE
________________________________________________________________________