C 0x线程静态连接问题

前端之家收集整理的这篇文章主要介绍了C 0x线程静态连接问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些问题试图使用c 0x线程功能静态链接程序.代码看起来这样((Compiler是 gcc 4.6.1在Debian x86_64测试)
#include <iostream>
#include <thread>

static void foo() {
  std::cout << "FOO BAR\n";
}

int main() {
  std::thread t(foo);
  t.join();
  return 0;
}

链接到:

g++ -static -pthread -o t-static t.cpp -std=c++0x

当我执行程序时,我有以下错误

terminate called after throwing an instance of 'std::system_error'
  what(): Operation not permitted
Aborted

GDB调试输出如下所示:

Debugger finished
Current directory is ~/testspace/thread/
GNU gdb (GDB) 7.2-debian
Copyright (C) 2010 Free Software Foundation,Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY,to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions,please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/will/testspace/thread/t-static...done.
(gdb) list -
1   #include <iostream>
(gdb) b 1
Breakpoint 1 at 0x4007c8: file t.cpp,line 1.
(gdb) r
Starting program: /home/will/testspace/thread/t-static 
terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted

Program received signal SIGABRT,Aborted.
0x00000000004a8e65 in raise ()
(gdb) bt
#0  0x00000000004a8e65 in raise ()
#1  0x000000000045df90 in abort ()
#2  0x000000000044570d in __gnu_cxx::__verbose_terminate_handler() ()
#3  0x0000000000442fb6 in __cxxabiv1::__terminate(void (*)()) ()
#4  0x0000000000442fe3 in std::terminate() ()
#5  0x0000000000443cbe in __cxa_throw ()
#6  0x0000000000401fe4 in std::__throw_system_error(int) ()
#7  0x00000000004057e7 in std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) ()
#8  0x0000000000400b18 in std::thread::thread<void (&)()> (this=0x7fffffffe540,__f=@0x4007c4) at /usr/include/c++/4.6/thread:135
#9  0x00000000004007f3 in main () at t.cpp:11
(gdb)

更新:

与静态libstdc链接可能(可能)使此错误消失,并且编译的C 0x程序可以在没有gcc 4.6 libs的系统上运行:

g++ -static-libgcc -pthread -L.-o t thread.cpp -std=c++0x

但是,首先,我们应该在当前目录下创建一个符号链接到“libstdc .a”:

ln -s `g++ -print-file-name=libstdc++.a`

(参考:http://www.trilithium.com/johan/2005/06/static-libstdc/)

解决方法

因为我完全不知道的原因(我认为这是一个错误),因为函数__ghtread_active_p()将被内联返回false(看看_M_start_thread的装配体),所以不能在gcc 4.6上使用std :: thread,导致这个异常被抛出.它可能是需要pthread_create函数的弱符号,当静态链接它们不在那里时,但是为什么他们不这样做是超出我的(注意,程序集以后包含像callq 0x0这样的东西,似乎有去某事非常错误).

现在我个人使用boost :: threads,因为我正在使用boost …

原文链接:https://www.f2er.com/c/110564.html

猜你在找的C&C++相关文章