Linux 使用 g++ 编译多线程代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19463602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compiling multithread code with g++
提问by zerkms
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. Only g++
package was installed and minor things like chromium
etc
PS:这是一个全新的 ubuntu 13.10 安装。只有g++
包安装和次要之类的东西chromium
等
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:
购买力平价:
$ dpkg --get-selections | grep 'libc.*dev'
libc-dev-bin install
libc6-dev:amd64 install
libclang-common-dev install
linux-libc-dev:amd64 install
采纳答案by zerkms
The answer was provided by a kind member of SO C++ chat.
答案是由SO C++ chat 的一位好心成员提供的。
It looks like this behaviour is caused by a bugin gcc.
看起来这种行为是由gcc 中的错误引起的。
The workaround provided in the last comment of that bug discussion doeswork and solves the issue:
该错误讨论的最后一条评论中提供的解决方法确实有效并解决了问题:
-Wl,--no-as-needed
回答by Petr Vep?ek
Adding -lpthread
fixed the identical problem for me:
添加-lpthread
为我修复了相同的问题:
g++ -std=c++11 foo.cpp -lpthread -o foo
回答by ecoretchi
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
回答by user31264
I have slightly more advanced version (4.8.4 instead of 4.8.1), and I tested all three answers above. In fact:
我有稍微更高级的版本(4.8.4 而不是 4.8.1),我测试了上面的所有三个答案。实际上:
-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
单独不行。
-lpthread
alone does not work.
-lpthread
单独不行。
-Wl,--no-as-needed
and -lpthread
togetherwork:
-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,--no-as-needed 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”。