使用 g++ 编译多线程代码 [英] Compiling multithread code with g++

查看:53
本文介绍了使用 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;
}

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

though I still cannot compile it with g++ to run.

更多详情:

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

现在我陷入了困境.在 Internet 上的每个相关线程中,建议在我已经拥有 -pthread 时添加它.

And now I'm in stuck. In every related thread over the internet it's recommended to add -pthread while I have it already.

我做错了什么?

PS:这是一个全新的 ubuntu 13.10 安装.只安装了 g++ 包和诸如 chromium 等小东西

PS: It's a brand new ubuntu 13.10 installation. Only g++ package was installed and minor things like chromium etc

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) 编译并运行良好

PPPS: with clang++ (v3.2) it compiles and runs fine

PPPPS:伙计们,这不是 什么是正确的linux下在GCC中使用std::thread的链接选项?

PPPPS: guys, it's not a duplicate of What is the correct link options to use std::thread in GCC under linux?

PPPS:

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

推荐答案

答案由 的好心成员提供SO C++ 聊天.

看起来这种行为是由错误 在 gcc 中.

It looks like this behaviour is caused by a bug in gcc.

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

The workaround provided in the last comment of that bug discussion does work and solves the issue:

-Wl,--no-as-needed

这篇关于使用 g++ 编译多线程代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆