0

我目前正在使用 Visual Studio 2022 和 clang-cl(12.0.0(现在与 VS 一起提供)和 13.0.0)实现我自己的 CRT,并且在使用时,/NODEFAULTLIB尽管实现了它们,但我在某些变量/函数上得到了未定义的符号。

// main.cpp
thread_local static int32_t test{};

int32_t __stdcall DllMain(HMODULE self, uint32_t reason, void* reserved)
{
    switch(reason)
    {
        case DLL_PROCESS_ATTACH:
        {
            ++test;
            return 0;
        }

        case DLL_PROCESS_DETACH:
        {
            return 1;
        }
    }

    return 0;
}

// crt.cpp
extern "C"
{
    // TLS index for this binary.
    uint32_t _tls_index{};
}

extern "C" int32_t __stdcall _DllMainCRTStartup(HMODULE self, uint32_t reason, void* reserved)
{
    // ...CRT init here...
    // Call into the client's entry point.
    const auto res = DllMain(self, reason, reserved);
    return res;
}

这会导致编译时出现此错误:

1>lld-link : error : undefined symbol: __tls_index
1>>>> referenced by [redacted]\src\crt.cpp:44
1>>>>               [redacted].dll.lto.1.obj:(__DllMainCRTStartup@12)
1>>>> referenced by [redacted]\src\main.cpp:11
1>>>>               [redacted].dll.lto.2.obj:(_DllMain@12)

知道为什么会这样吗?是否有我忘记使用 clang-cl 的特定编译器选项?这一切在 MSVC 下都可以正常工作(发现符号没有问题)。

4

0 回答 0