【问题标题】:cpp thread program can't run without -fprofile-arcs没有-fprofile-arcs cpp线程程序无法运行
【发布时间】:2013-10-30 04:36:10
【问题描述】:

这是 ubuntu 13.10 和 g++ 4.8 的错误,所以我关闭了这个问题。

within -Wl,--no-as-needed.

我的脑袋有问题。 我有一些类似的代码

#include <iostream>
#include <thread>

int main(int argc, char* argv[])
{
    {
        std::thread t1([&]{ std::cout << "hello " << std::endl; });
        t1.join();        
    }
    return 0;
}

这是我的编译命令:

g++ -std=c++0x -lpthread test.cpp

ps:我已经更改了所有类型的链接顺序。

它适用于 g++ 4.7 和 ubuntu 13.04 但它会在 g++ 4.8.1 和 ubuntu 12.10 上引发 system_error

直到我用 -fprofile-arcs 编译它运行良好。

就在下面:

g++ test.cpp -std=c++0x -fprofile-arcs -pthread
a.out
hello

g++ test.cpp -std=c++0x  -pthread
a.out
terminate called after throwing an instance of 'std::system_error'

what(): Enable multithreading to use std::thread: Operation not allowed

【问题讨论】:

  • 编译或链接是否失败?
  • 假设是链接,导致运行时出错
  • 编译为g++ -std=c++0x test.cpp -lpthread 时工作正常。编译为g++ -std=c++0x -lpthread test.cpp 时抛出“操作不允许”系统异常
  • 仍然“不允许操作”
  • 你需要使用编译器标志-pthred(不要用-lpthread链接到libpthread)。

标签: c++ multithreading c++11 compiler-construction


【解决方案1】:

只需搭建一个 Ubuntu 12.10 虚拟机

osmith@ubuntu1210 ~/src $ g++-4.8 --version
g++ (Ubuntu 4.8.1-2ubuntu1~12.10) 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.

并用这个命令行编译:

osmith@ubuntu1210 ~/src $ g++-4.8 -std=c++11 -o test.exe test.cpp -pthread -lpthread
osmith@ubuntu1210 ~/src $ ./test.exe
hello 

【讨论】:

  • 是的,我的 ubuntu 13.04 也可以,但是在 13.10 中运行 fialue。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 2016-10-03
  • 2017-09-02
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多