是否保证__attribute __((constructor))被精确地调用一次?

pto*_*ato 5 c gcc shared-libraries

是否可以确保使用__attribute__((constructor))和定义的GCC共享库构造函数和析构函数__attribute__((destructor))只运行一次?该文档似乎暗示它们将至少运行一次,但没有提及超过一次。

换句话说,如果我在构造函数中执行的操作仅必须执行一次,那么是否需要像这样来保护它:

static gboolean constructor_has_run = FALSE;
if(!constructor_has_run) {
    do_operation();
    constructor_has_run = TRUE;
}
Run Code Online (Sandbox Code Playgroud)

小智 1

如果使用__attribute__((constructor))它将在执行开始时被调用。

所以你不必像上面提到的那样进行保护。

如果你也提到它并没有错

有关更多信息, __attribute__((constructor))您可以查看https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html