如何在Qt Creator上使用pthread
How to use pthread on Qt Creator
我想执行以下代码。
#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
相关文章:
- 无法使用Qt Creator在Windows中构建yaml-cpp
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 如何在QT Creator上将QWidget声明为继承类的对象
- 在 QTextEdit 中使用指针或在 Qt-Creator 上使用 connect()
- Qt creator 4.11,在应用程序输出面板中创建一个链接
- 在 c++11 中为 pthread 设置调度参数
- 使用 Cmake 在 Qt Creator 中显示头文件
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- 如何在 CMakeLists.txt Qt Creator 中包含头文件?
- pthread只有在线程数量较少时才可以正常工作
- 为什么Qt Creator的应用程序输出不能从spdlog记录器打印
- 使用Qt Creator在调试模式下编译一段代码
- pthread执行时间比顺序执行时间差
- 使用 -pthread 如何不违反 ODR 规则?
- 使用 pthread 的多线程
- Qt Creator 在执行步骤 "make" 时出现编译错误,-fno-stack-limit
- Qt creator语言 - fno-elide-constructors 不起作用
- 如何将Valgrind与Qt Creator一起使用来调试远程应用程序?
- 销毁 pthread 互斥体和 C++ 中的取消初始化顺序
- 如何在Qt Creator上使用pthread