How to use pthread on Qt Creator

匿名 (未验证) 提交于 2019-12-03 01:31:01

问题:

I want to execute a following code.

#include  #include   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

However I can execute on the terminal using the option of -pthread. Could you tell me how to use the option of -pthread in Qt Creator?

My developing environment is Ubuntu(12.04), g++4.6.3, Qt Creator(2.4.1).

Thank you.

回答1:

You also need to link against -pthread. If you use g++ main.cpp -std=c++0x -pthread you are doing all that in one step, so it works correctly. To make Qt do the correct things, add the following to your project file:

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


回答2:

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.

Try save your example as test.cpp, and the above as project.pro in same directory. Then type:

$ qmake $ make $ ./project Hello World


回答3:

Add your command line arguments here: http://doc.qt.nokia.com/qtcreator-2.4/creator-run-settings.html



转载请标明出处:How to use pthread on Qt Creator
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!