我有史以来最简单的代码:

#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

run :

$ ./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: with clang++ (v3.2) it compiles and runs fine

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

PPPPPS:

$ 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++ chat人提供的.

看起来这种行为是由gcc中的bug导致的.

错误讨论does的最后一条 comments 中提供的解决方法有效并解决了问题:

-Wl,--no-as-needed

Linux相关问答推荐

Bash脚本用于在远程工作后关闭用户会话

如何确定Linux上的最大静态TLS(线程本地存储)块大小?

仅使用Find搜索32字符长的目录

Git - 打印以不同编码混合的文件

如何使用换行符或制表符或空格 grep 多行字符串

std::system 使用什么Shell?

如何增加 /proc/pid/cmdline 4096 字节限制?

Bash 变量:是否区分大小写?

适用于 Linux 和 Mac 的 HTTP 调试代理

C hello world 的汇编输出的每一行是什么意思?

如何通过 ssh 判断 ubuntu 服务器上是否存在 php 和 apache

为什么可执行文件操作系统依赖于?

zsh/bash 上不区分大小写的 Glob

将 CMake 项目导入 Eclipse CDT

发出信号并中断比较

根据文件名模式和文件内容列出文件名?

从bash中的字符串中删除所有特殊字符和大小写

如何使用 bash 在文件中间添加一行文本?

bash / Makefile中双美元符号的含义是什么?

如何将 ISO8859-15 转换为 UTF8?