我正在尝试 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 上)。你知道为什么设置优先级数字会导致应用程序崩溃吗?
在此阶段 std::err 很可能未初始化。不要将
iostream
用于构造函数。使用printf
家庭。 C 运行时由调用构造函数时初始化。