dlopen() not initializing modules?
Charles Y. Kim
ckim@telcontar.com
Thu Jun 13 16:20:00 GMT 2002
So does anyone know if there's a magic linker flag to initialize static
globals in shared objects using GCC3.0.4 under HP-UX11?
Take the following super simple case:
class myClass {
public:
int x;
myClass() {
x = 5;
}
};
myClass test;
extern "C" void run()
{
cout << test.x << endl;
}
I dlopen() the file that contains this code, use dlsym to get a pointer to
run(), and then run the function. The myClass constructor is never run so
when I print out test.x I get 0 instead of 5!
How do I fix this?
Thanks.
- Charles
-----Original Message-----
From: Charles Y. Kim [mailto:ckim@telcontar.com]
Sent: Monday, June 03, 2002 10:32 AM
To: 'gcc-help@gcc.gnu.org'
Subject: dlopen() not initializing modules?
Hi all,
When I dlopen() a shared object, it looks like the module initializations
aren't being performed and the constructors for globals aren't getting
called (I verified this with a small test program).
Does anyone know how I might fix this?
Here's my test code:
#include <map>
#include <string>
#include <stdio.h>
#include <iostream.h>
class myMap {
public:
void add(int x, int y) {
m[x] = y;
}
myMap() {
puts("Constructor run");
}
private:
std::map<int, int> m;
};
//myMap m2;
extern "C" int run() {
myMap m2;
m2.add(1, 2);
}
This shared object is being opened like this:
int main() {
void *handle;
void (*function)();
handle = dlopen("./maptest.so", RTLD_LAZY);
char *err;
err = dlerror();
if(err != NULL)
cout << "File Load failure: " << err << endl;
function = (void(*)())dlsym(handle, "run");
err = dlerror();
if(err != NULL)
cout << "Load failure: " << err << endl;
else {
for (int i = 0; i < 1000000000; i++) {
cout << "Count: " << i << endl;
function();
}
}
}
I'm dlopen()ing this file built as a shared object. When I declare a myMap
in global, the constructor is never run, and I end up with a segmentation
fault/core dump.
Anyone have any ideas? I'm running GCC3.0.4 on HP-UX11i.
Thank in advance.
- Charles
More information about the Gcc-help
mailing list