【问题标题】:Compiling multithread code with g++用g++编译多线程代码
【发布时间】:2013-10-28 02:52:58
【问题描述】:

我有最简单的代码:

#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;
}

虽然我仍然无法用g++ 编译它来运行。

更多细节:

$ 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.

编译命令:

$ g++ main.cpp -o main.out -pthread -std=c++11

跑步:

$ ./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)

现在我陷入了困境。建议在互联网上的每个相关线程中添加-pthread,而我已经拥有它。

我做错了什么?

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:使用 clang++ (v3.2) 编译并运行良好

PPPPS:伙计们,这不是What is the correct link options to use std::thread in GCC under linux?的复制品

PPPPS:

$ dpkg --get-selections | grep 'libc.*dev'
libc-dev-bin                    install
libc6-dev:amd64                 install
libclang-common-dev             install
linux-libc-dev:amd64                install

【问题讨论】:

  • 这是指向同一目录中libpthread-2.17.so 的符号链接。
  • 是的,这是一个正确的链接。这是我的目录 lrwxrwxrwx。 1 根 18 Sep 12 20:52 libpthread.so.0 -> libpthread-2.16.so
  • @Jason Enochs:这对我有用——我同意,如果我不使用有缺陷的 gcc 版本
  • 是的,你所有的库都比我的更新一点:gcc 版本 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC)
  • @Jason Enochs:呵呵。对我自己来说,我已经对“这是一个错误”的答案感到满意,明天可能会尝试在较旧的 ubuntu 上运行它,只是为了完全确定

标签: c++ linux ubuntu gcc g++


【解决方案1】:

添加 -lpthread 为我解决了同样的问题:

 g++ -std=c++11 foo.cpp -lpthread -o foo

【讨论】:

  • Jep,这是答案,也是完全合理的。 “-Wl,--no-as-needed”听起来很奇怪,对我也不起作用。
  • 你肯定需要-lpthread。您可能需要-pthread -Wl,--no-as-needed,具体取决于编译器版本。 gcc-4.8.2 需要它。
【解决方案2】:

我有更高级的版本(4.8.4 而不是 4.8.1),并且我测试了上面的所有三个答案。事实上:

-pthread 单独工作:

g++ -std=c++11 -o main -pthread main.cpp

-Wl,--no-as-needed 单独不起作用

-lpthread 单独不起作用

-Wl,--no-as-needed-lpthread 一起工作:

g++ -std=c++11 -o main -Wl,--no-as-needed main.cpp -lpthread

我的版本是“g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4”。

【讨论】:

  • 好吧,解决方案是针对不同版本提供的,我可以保证检查的答案在我检查它的那一刻确实有效。无论如何,感谢您提供实际信息。
【解决方案3】:

答案是SO C++ chat的好心人提供的。

看起来这种行为是由 gcc 中的 bug 引起的。

该错误讨论的最后一条评论中提供的解决方法确实有效并解决了问题:

-Wl,--no-as-needed

【讨论】:

  • 如果没有 SO 和像你这样的人,我该怎么办?你救了我一头头发。谢谢 :) 这就像它得到的一样晦涩难懂(至少对我来说)
  • @scorpiodawg:你的评论让我度过了一个愉快的夜晚,谢谢 :-)
  • 我必须添加所有 -pthread -lpthread -Wl,--no-as-needed 标志才能完成这项工作。
【解决方案4】:

已经回答了 对于 qtcreator:

LIBS += -pthread
QMAKE_CXXFLAGS += -pthread
QMAKE_CXXFLAGS += -std=c++11

对于控制台 g++:here

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

【讨论】:

  • 谢谢,您的回答是唯一清楚表明 -pthread 必须传递给编译器和链接器的答案。
猜你喜欢
  • 1970-01-01
  • 2015-11-05
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 2014-05-25
  • 1970-01-01
  • 2011-09-16
相关资源
最近更新 更多