带有c++ 11线程的Qtcreator

Qtcreator with C++11 thread

本文关键字:Qtcreator 线程 c++ 带有      更新时间:2023-10-16

我为c++ 11线程编写了hello world程序。我正在使用qtcreator ide与gcc 4.9.1,但我无法运行该程序。它可以正确编译。但是当我运行时,它给出以下警告

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

我的项目。Pro文件

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
QMAKE_CXXFLAGS += -std=c++11
LIBS += -pthread

and main.cpp is

#include <iostream>
#include <thread>
void hello()
{
    std::cout<<"Hello Concurrent Worldn";
}
int  main()
{
    std::thread t(hello);
    t.join();
}

我注意到它使用了

g++ -c -pipe -std=c++11 -g -Wall -W -fPIE  -I/usr/lib/qt/mkspecs/linux-g++ -I. -o main.o main.cpp

创建main。0对象文件和

g++ -Wl,-O1,--sort-common,--as-needed,-z,relro -o project main.o   -pthread 

创建可执行文件。如果从用于创建可执行文件的命令中删除——as-needed。它工作得很好。请让我知道正确的解决方案,以便我可以使用qtcreator为c++ 11线程。

这是libstdc++中的一个bug。作为解决方法,如果使用c++线程,请不要使用--as-needed