[英]Compiling multithread code with g++
I have the easiest code ever: 我有史以来最简单的代码:
#include <iostream>
#include <thread>
void worker()
{
std::cout << "another thread";
}
int main()
{
std::thread t(worker);
std::cout << "main thread" << std::endl;
t.join();
return 0;
}
though I still cannot compile it with g++
to run. 尽管我仍然无法使用g++
进行编译以使其运行。
More details: 更多细节:
$ g++ --version
g++ (Ubuntu/Linaro 4.8.1-10ubuntu8) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Command to compile: 编译命令:
$ g++ main.cpp -o main.out -pthread -std=c++11
Running: 正在运行:
$ ./main.out
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)
And now I'm in stuck. 现在我陷入困境。 In every related thread over the internet it's recommended to add -pthread
while I have it already. 在互联网上的每个相关线程中,建议在已经拥有的情况下添加-pthread
。
What am I doing wrong? 我究竟做错了什么?
PS: It's a brand new ubuntu 13.10 installation. PS:这是全新的ubuntu 13.10安装。 Only g++
package was installed and minor things like chromium
etc 仅安装了g++
软件包,并安装了chromium
等次要东西
PPS: PPS:
$ ldd ./a.out
linux-vdso.so.1 => (0x00007fff29fc1000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb85397d000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb853767000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb85339e000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb85309a000)
/lib64/ld-linux-x86-64.so.2 (0x00007fb853c96000)
PPPS: with clang++
(v3.2) it compiles and runs fine PPPS:使用clang++
(v3.2),它可以编译并正常运行
PPPPS: guys, it's not a duplicate of What is the correct link options to use std::thread in GCC under linux? PPPPS:伙计们,这不是Linux下在GCC中使用std :: thread的正确链接选项是什么的重复项?
PPPPPS: PPPPPS:
$ dpkg --get-selections | grep 'libc.*dev'
libc-dev-bin install
libc6-dev:amd64 install
libclang-common-dev install
linux-libc-dev:amd64 install
The answer was provided by a kind member of SO C++ chat . 答案是由SO C ++聊天的一位好心人提供的。
It looks like this behaviour is caused by a bug in gcc. 看来此行为是由gcc中的错误引起的。
The workaround provided in the last comment of that bug discussion does work and solves the issue: 该错误讨论的最后评论中提供的解决方法确实可以解决该问题:
-Wl,--no-as-needed
添加-lpthread
为我解决了相同的问题:
g++ -std=c++11 foo.cpp -lpthread -o foo
I have slightly more advanced version (4.8.4 instead of 4.8.1), and I tested all three answers above. 我有更高级的版本(4.8.4而不是4.8.1),并且我测试了以上所有三个答案。 In fact: 事实上:
-pthread
alone works: -pthread
单独工作:
g++ -std=c++11 -o main -pthread main.cpp g ++ -std = c ++ 11 -o main -pthread main.cpp
-Wl,--no-as-needed
alone does not work . -Wl,--no-as-needed
仅-Wl,--no-as-needed
是行不通的 。
-lpthread
alone does not work . 单独使用-lpthread
无效 。
-Wl,--no-as-needed
and -lpthread
together work: -Wl,--no-as-needed
和-lpthread
一起工作:
g++ -std=c++11 -o main -Wl,--no-as-needed main.cpp -lpthread g ++ -std = c ++ 11 -o main -Wl,-无需按需main.cpp -lpthread
My version is "g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4". 我的版本是“ g ++(Ubuntu 4.8.4-2ubuntu1〜14.04.1)4.8.4”。
answer already was made for qtcreator: 已经为qtcreator做出了答案:
LIBS += -pthread
QMAKE_CXXFLAGS += -pthread
QMAKE_CXXFLAGS += -std=c++11
for console g++: here 对于控制台g ++: 在这里
g++ -c main.cpp -pthread -std=c++11 // generate target object file
g++ main.o -o main.out -pthread -std=c++11 // link to target binary
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.