In my shared library I need to load some data into an unordered_map, and I try to do it in a function marked __ attribute__((constructor)). However I got the SIGFPE on every map operation. After some looking into stackoverflow I found that this means that unordered_map is uninitialized. This is quite unexpected and ununderstandable for me, because, at a glance, it violates C++ contracts. Anybody could help on how can I run this method after constructors were run? Here is a working example with my own constructor, which shows that it's not called:
#include <stdio.h>
class Ala {
int i;
public:
Ala() {
printf("constructor called\n");
i = 3;
}
int getI() {
return i;
}
};
Ala a;
__attribute__((constructor))
static void initialize_shared_library() {
printf("initializing shared library\n");
printf("a.i=%d\n", a.getI());
printf("end of initialization of the shared library\n");
}
The outcome is
initializing shared library
a.i=0
end of initialization of the shared library
constructor called
however if one tries to use std::cout instead of printfs then it goes into SEGFAULTs immediately (because constructor of streams were not run)