如何使用posix线程在Windows上使用std ::线程

发布于 2025-02-02 19:09:05 字数 991 浏览 5 评论 0 原文

一直在尝试编译并运行以下非常简单的代码。

#include <thread>

using namespace std;

void thread_c() {
    for (int i = 0; i < 11; ++i) {
        cout << i;
    }
}

int main()
{
    thread t(thread_c);
    t.join();

    system("pause");
    return 0;
}

我一直在尝试使用MingW 6.3.0(Target X86_64-W64-MingW32)和线程模型(使用 g ++ -v )是posix。

I am not getting any compilation error with g++ -Wall -g test.cpp -o test.exe, however I am getting a runtime error when trying to run the exe (entry point of _ZNSt6thread15_M_start_threadESt10unique_ptrINS_3_StateESt14default_deleteIS1_EEPFvve找不到)。

我还尝试使用 -pthread -lpthread 标志来编译,但是我遇到了相同的运行时错误。 显然,这似乎与使用std ::线程有关,但是我没有得到解决方案。我是否缺少汇编标志或其他库,从而在Windows上启用POSIX线程支持?

编辑:我设法通过将汇编命令更改为 g ++ -wall -g test.cpp -o test.exe -static -libgcc -static -libstdc ++ -wl,-bstatic -lstdc ++ -lpthread -lpthread -wl,bdynamic

I have been trying to compile and run the following very simple bit of code

#include <thread>

using namespace std;

void thread_c() {
    for (int i = 0; i < 11; ++i) {
        cout << i;
    }
}

int main()
{
    thread t(thread_c);
    t.join();

    system("pause");
    return 0;
}

I am compiling on Windows 10 with MinGW 6.3.0 (target x86_64-w64-mingw32) and the thread model (obtained using g++ -v) is POSIX.

I am not getting any compilation error with g++ -Wall -g test.cpp -o test.exe, however I am getting a runtime error when trying to run the exe (entry point of _ZNSt6thread15_M_start_threadESt10unique_ptrINS_3_StateESt14default_deleteIS1_EEPFvve cannot be found).

I also tried compiling with the -pthreador -lpthread flags, but I am getting the same runtime error.
Obviously this seems to be related to the use of std::thread, but I didn't get how to fix this. Am I missing a compilation flag or additional library enabling POSIX thread support on Windows?

Edit: I managed to get it working by changing the compilation command to
g++ -Wall -g test.cpp -o test.exe -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,Bdynamic

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2025-02-09 19:09:05

进行必要的修复后,我尝试了您的代码:

#include <thread>
#include <iostream>

using namespace std;

void thread_c() {
    for (int i = 0; i < 11; ++i) {
        std::cout << i;
    }
}

int main()
{
    thread t(thread_c);
    t.join();

    system("pause");
    return 0;
}

它运行良好。
构建命令与您的相同:

g++ -Wall -g test.cpp -o test.exe

输出是:

012345678910Press any key to continue . . .

但是我确实使用了mingw-w64 gcc 12.1.0(for ),而不是非常旧的6.3.0版。

也许您正在使用的Mingw-w64(还是它仍然是旧的mingw库)太老了...

I tried your code after making the necessary fixes:

#include <thread>
#include <iostream>

using namespace std;

void thread_c() {
    for (int i = 0; i < 11; ++i) {
        std::cout << i;
    }
}

int main()
{
    thread t(thread_c);
    t.join();

    system("pause");
    return 0;
}

and it worked fine.
The build command was the same as yours:

g++ -Wall -g test.cpp -o test.exe

The output was:

012345678910Press any key to continue . . .

But I did use MinGW-w64 GCC 12.1.0 (fro https://winlibs.com/) instead of the very old version 6.3.0.

Maybe the MinGW-w64 (or was it still the old MinGW) library you were using was simply too old...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文