用g ++编译多线程代码

zer*_*kms 85 c++ linux ubuntu gcc g++

我有最简单的代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

虽然我仍然无法编译它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.
Run Code Online (Sandbox Code Playgroud)

编译命令:

$ g++ main.cpp -o main.out -pthread -std=c++11
Run Code Online (Sandbox Code Playgroud)

运行:

$ ./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)
Run Code Online (Sandbox Code Playgroud)

现在我陷入了困境.在互联网上的每个相关主题中,建议-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)
Run Code Online (Sandbox Code Playgroud)

PPPS:用clang++(v3.2)编译并运行正常

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
Run Code Online (Sandbox Code Playgroud)

zer*_*kms 74

答案是由SO C++聊天的一个成员提供的.

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

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

-Wl,--no-as-needed
Run Code Online (Sandbox Code Playgroud)

  • 如果没有SO,我会做什么?你救了我一头头发.谢谢:)这就像它得到的一样模糊(至少对我来说) (8认同)
  • 我必须添加所有“-pthread -lpthread -Wl,--no-as-needed”标志才能完成这项工作。 (2认同)

Pet*_*řek 29

添加-lpthread为我修复了相同的问题:

 g++ -std=c++11 foo.cpp -lpthread -o foo
Run Code Online (Sandbox Code Playgroud)

  • 你肯定需要`-lpthread`.您*可能*需要`-pthread -Wl, - no-as-needed`,具体取决于编译器版本.**gcc-4.8.2**需要它. (3认同)

use*_*264 12

我有更高级的版本(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".


小智 9

回答已经为qtcreator做了:

LIBS += -pthread
QMAKE_CXXFLAGS += -pthread
QMAKE_CXXFLAGS += -std=c++11
Run Code Online (Sandbox Code Playgroud)

对于控制台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
Run Code Online (Sandbox Code Playgroud)