为什么 __attribute((constructor)) 有效,而 __attribute((constructor(100))) 不能(段错误)使用 clang++ 17.0.1

问题描述 投票:0回答:1

我正在尝试 clang 中提供的

constructor
属性(此处为 clang++)。当我这样做时:

#include <iostream>

__attribute__((constructor)) void RegistryFunction();

void RegistryFunction() {
    std::cerr << "working" << std::endl;
}

int main() {
    std::cerr << "Main function running." << std::endl;
    return 0;
}

编译并运行良好

clang++ -std=c++23 -c test.exe text.cc

但是如果我像这样使用

constructor(100)
(设置优先级,可以是0或其他任何值),就会出现段错误。

我使用的是 clang 17.0.1(在 Windows 上)。你知道为什么设置优先级数字会导致应用程序崩溃吗?

c++ clang++
1个回答
0
投票

在此阶段 std::err 很可能未初始化。不要将

iostream
用于构造函数。使用
printf
家庭。 C 运行时由调用构造函数时初始化。

© www.soinside.com 2019 - 2024. All rights reserved.