简体   繁体   English

如何在Qt Creator上使用pthread

[英]How to use pthread on Qt Creator

I want to execute a following code. 我要执行以下代码。

#include <iostream>
#include <thread>

void output() {
    std::cout << "Hello World" << std::endl;
}

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

    return 0;
}

I can't execute it. 我无法执行。

Qt Creator outputs terminate called after throwing an instance of 'std::system_error' what(): Operation not permitted 抛出'std :: system_error'what()实例之后,Qt Creator输出终止被调用what():不允许的操作

However I can execute on the terminal using the option of -pthread. 但是我可以使用-pthread选项在终端上执行。 Could you tell me how to use the option of -pthread in Qt Creator? 您能告诉我如何在Qt Creator中使用-pthread选项吗?

My developing environment is Ubuntu(12.04), g++4.6.3, Qt Creator(2.4.1). 我的开发环境是Ubuntu(12.04),g ++ 4.6.3,Qt Creator(2.4.1)。

Thank you. 谢谢。

You also need to link against -pthread . 您还需要针对-pthread进行链接。 If you use g++ main.cpp -std=c++0x -pthread you are doing all that in one step, so it works correctly. 如果您使用g++ main.cpp -std=c++0x -pthread那么您将一步完成所有操作,因此它可以正常工作。 To make Qt do the correct things, add the following to your project file: 为了使Qt做正确的事,将以下内容添加到您的项目文件中:

QMAKE_CXXFLAGS += -std=c++0x -pthread 
LIBS += -pthread

This works for me: 这对我有用:

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .

# Input
SOURCES += test.cpp

QMAKE_CXXFLAGS += -std=gnu++0x -pthread
QMAKE_CFLAGS += -std=gnu++0x -pthread

Your example compiles and executes correctly with the above .pro file on my system. 您的示例使用我的系统上的上述.pro文件编译并正确执行。

Try save your example as test.cpp, and the above as project.pro in same directory. 尝试将示例另存为test.cpp,将上面的另存为project.pro。 Then type: 然后输入:

$ qmake
$ make
$ ./project
Hello World

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM