GCC __attribute __((构造函数))在对象构造函数之前调用

paw*_*l_j 2 c++ gcc

在我的共享库中,我需要将一些数据加载到unordered_map中,并尝试在标记为__ attribute __((构造函数)的函数中执行此操作.但是我在每个地图操作上都获得了SIGFPE.在查看stackoverflow之后,我发现这意味着unordered_map未初始化.这对我来说是非常意外和不可理解的,因为它一眼就违反了C++合同.任何人都可以帮助我在运行构造函数后如何运行此方法?这是一个使用我自己的构造函数的工作示例,它表明它没有被调用:

#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");
}
Run Code Online (Sandbox Code Playgroud)

结果是

initializing shared library
a.i=0
end of initialization of the shared library
constructor called
Run Code Online (Sandbox Code Playgroud)

但是如果有人试图使用std :: cout而不是printfs,那么它会立即进入SEGFAULTs(因为没有运行流的构造函数)

Que*_*tin 6

__attribute__((constructor))是一个编译器扩展,所以你离开了标准C++的领域.看起来GCC的构造函数在全局初始化之前运行.

修复它的方法是使用另一个vanilla C++构造,例如一个全局对象,通过在与其他全局相同的TU中定义它来正确地对其进行排序:

Ala a;

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");
}

static int const do_init = (initialize_shared_library(), 0);
Run Code Online (Sandbox Code Playgroud)

  • 可以将其称为"static struct init {init(){/*do init*/}} force_init;` - 这可以避免使用稍微棘手的逗号运算符. (2认同)