如何在Qt Creator上使用pthread

How to use pthread on Qt Creator

本文关键字:pthread Creator Qt      更新时间:2023-10-16

我想执行以下代码。

#include <iostream>
#include <thread>
void output() {
    std::cout << "Hello World" << std::endl;
}
int main()
{
    std::thread t(output);
    t.join();
    return 0;
}

我无法执行。

Qt创建者输出在抛出"std::system_error"的实例后调用terminatewhat():不允许操作

但是,我可以使用-phread选项在终端上执行。你能告诉我如何在Qt Creator中使用-phread选项吗?

我的开发环境是Ubuntu(12.04),g++4.6.3,Qt Creator(2.4.1)。

谢谢。

您还需要针对-pthread进行链接。如果您使用g++ main.cpp -std=c++0x -pthread,您将在一个步骤中完成所有这些操作,因此它可以正常工作。为了让Qt做正确的事情,请将以下内容添加到您的项目文件中:

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

这对我有效:

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .
# Input
SOURCES += test.cpp
QMAKE_CXXFLAGS += -std=gnu++0x -pthread
QMAKE_CFLAGS += -std=gnu++0x -pthread

您的示例在我的系统上使用上面的.pro文件正确编译和执行。

尝试将您的示例保存为test.cpp,并将上面的示例保存在同一目录中的project.pro。然后类型:

$ qmake
$ make
$ ./project
Hello World

在此处添加命令行参数:http://doc.qt.nokia.com/qtcreator-2.4/creator-run-settings.html