Linux - 设置Code :: Blocks来编译多线程C ++代码

时间:2022-01-07 08:30:56

I have searched through forums here, but nothing helped to the end in my case. I'm trying to write and compile some C++ multithreading code in Code::Blocks 13.12, for now the only code I have is:

我在这里搜索了论坛,但在我的案例中没有任何帮助。我正在尝试编写和编译Code :: Blocks 13.12中的一些C ++多线程代码,现在我唯一的代码是:

#include <iostream>
#include <thread>

void cpp11()
{
    std::cout<<"C++11 MULTITHREADING\n";
}

int main()
{
    std::thread t(cpp11);
    t.join();
}

Initially I could not compile and run that code (both in Code::Blocks and Terminal) because I was getting an error:

最初我无法编译和运行该代码(在Code :: Blocks和Terminal中),因为我收到了一个错误:

Terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted.

在抛出'std :: system_error'的实例后终止调用what():启用多线程以使用std :: thread:不允许操作。

I found that I'm able to compile and run that code calling g++ in the Terminal like that:

我发现我能够编译并运行在终端中调用g ++的代码:

g++ -std=c++11 -pthread main.cpp -o Program

but I cannot set the same in Code::Blocks. I went to Project -> Build options, in the Compiler settings tab I selected Have g++ follow the C++11 ISO C++ language standard [-std=c++11]. In the Compiler settings -> Other options I typed -pthread. In Linker settings -> Other linker options I typed -pthread. It does not work, after I build and run my project in Code::Blocks I get the same error message.

但我不能在Code :: Blocks中设置相同的内容。我去了Project - > Build options,在我选择的编译器设置选项卡中,让g ++遵循C ++ 11 ISO C ++语言标准[-std = c ++ 11]。在编译器设置 - >其他选项中,我输入-pthread。在链接器设置 - >其他链接器选项中,我键入了-pthread。它不起作用,我在Code :: Blocks中构建并运行我的项目后得到相同的错误消息。

Thank you for any help!

感谢您的任何帮助!

1 个解决方案

#1


Follow instructions for this question

按照此问题的说明操作

How do I link to a library with Code::Blocks?

如何使用Code :: Blocks链接到库?

in the last step, when Add library dialog pops up type pthread.

在最后一步中,弹出添加库对话框时键入pthread。

UPDATE

Again go to build options > Compiler settings . click on other options and type -pthread

再次转到构建选项>编译器设置。单击其他选项并键入-pthread

#1


Follow instructions for this question

按照此问题的说明操作

How do I link to a library with Code::Blocks?

如何使用Code :: Blocks链接到库?

in the last step, when Add library dialog pops up type pthread.

在最后一步中,弹出添加库对话框时键入pthread。

UPDATE

Again go to build options > Compiler settings . click on other options and type -pthread

再次转到构建选项>编译器设置。单击其他选项并键入-pthread