C ++ 0x线程静态链接问题 [英] C++0x thread static linking problem

查看:131
本文介绍了C ++ 0x线程静态链接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一些问题,尝试静态链接程序使用c + + 0x线程功能。代码看起来如下:(编译器是gcc 4.6.1在Debian x86_64测试)

  #include< iostream> 
#include< thread>

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

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

我链接到:

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

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

 后抛出一个'std :: system_error'
的实例what():不允许操作
中止


b $ b

GDB调试输出如下:

 调试器完成
当前目录是〜/ testspace /线程/
GNU gdb(GDB)7.2-debian
版权所有(C)2010自由软件基金会,
许可证GPLv3 +:GNU GPL版本3或更高版本< http:// gnu。 org / licenses / gpl.html>
这是免费软件:您可以自由更改和重新分配。
在法律允许的范围内,没有任何保证。有关详细信息,请键入显示复制
和显示保修。
此GDB配置为x86_64-linux-gnu。
有关错误报告说明,请参阅:
< http://www.gnu.org/software/gdb/bugs/> ...
从/ home /中读取符号/testspace/thread/t-static...done。
(gdb)list -
1 #include< iostream>
(gdb)b 1
断点1在0x4007c8:文件t.cpp,行1.
(gdb)r
启动程序:/ home / will / testspace / thread / t-static
抛出'std :: system_error'实例后终止调用
what():不允许操作

编程接收到的信号SIGABRT,中止。
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(&)()> (这是= 0x7fffffffe540,__f = @ 0x4007c4)at /usr/include/c++/4.6/thread:135
#9 0x00000000004007f3在main()at t.cpp:11
(gdb)

更新:



使用静态libstdc ++链接)使这个错误消失,编译的C ++ 0x程序可以在没有gcc 4.6 libs的系统上运行:

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

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

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

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


<你可以使用-u解决问题(在gcc版本4.6.3 /(Ubuntu EGLIBC 2.15-0ubuntu10.4)2.15,gcc版本4.8.1(测试) /(Ubuntu EGLIBC 2.15-0ubuntu10.5〜ppa1)2.15)



-Wl,-u,pthread_cancel,-u,pthread_cond_broadcast,-u,pthread_cond_destroy, -u,pthread_cond_signal,-u,pthread_cond_wait,-u,pthread_create,-u,pthread_detach,-u,pthread_cond_signal,-u,pthread_equal,-u,pthread_join,-u,pthread_mutex_lock,-u,pthread_mutex_unlock,-u,pthread_once, -u,pthread_setcancelstate



1。重现错误



  g ++的-g -O0 -static -std = C ++ 11 t.cpp -lpthread启用多线程使用std ::螺纹:不允许操作

./a.out
终止扔'的std :: SYSTEM_ERROR'$ b $一个实例b什么()之后调用中止(内核转储)


nm a.out | egrep的\bpthread _。*$ B $体重调用pthread_cond_broadcast $ B $体重pthread_cond_destroy $ B $体重调用pthread_cond_signal $ B $体重调用pthread_cond_wait $ B $体重pthread_create的$ B $体重pthread_detach $ B $体重pthread_equal $ B $体重在pthread_join $ B $体重的pthread_mutex_lock $ b $体重调用pthread_mutex_unlock $ b $体重调用pthread_once $ b $体重pthread_setcancelstate

2。解决的bug



  g ++的-g -O0 -static -std = C ++ 11 t.cpp -lpthread -Wl,-u,pthread_join,-u,pthread_equal 
./a.out
FOO BAR


nm a.out | egrep的\bpthread _。*
0000000000406320牛逼pthread_cancel可以$ B $体重调用pthread_cond_broadcast $ B $体重pthread_cond_destroy $ B $体重调用pthread_cond_signal $ B $体重调用pthread_cond_wait
0000000000404970W¯¯pthread_create的$ B $体重pthread_detach
00000000004033e0ŧpthread_equal
00000000004061a0牛逼pthread_getspecific
0000000000403270ŧ在pthread_join
0000000000406100牛逼pthread_key_create
0000000000406160牛逼pthread_key_delete
00000000004057b0牛逼的pthread_mutex_lock
00000000004059c0牛逼pthread_mutex_trylock
0000000000406020ŧ调用pthread_mutex_unlock
00000000004063b0牛逼调用pthread_once $ b $体重pthread_setcancelstate
0000000000406220牛逼pthread_setspecific


I am having some issues trying to statically link programs using c++0x thread features. Code looks this: (Compiler is gcc 4.6.1 on Debian x86_64 testing)

#include <iostream>
#include <thread>

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

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

I link it with:

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

When I execute the program, I have the following error:

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

GDB Debug output looks like this:

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)

Update:

Linking with static libstdc++ could (possibly) make this error disappear, and the compiled C++0x programs can run on systems without gcc 4.6 libs:

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

But first, we should make a symbolic link to 'libstdc++.a' at current directory:

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

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

解决方案

you can use -u to resolve the problem (test in gcc version 4.6.3/(Ubuntu EGLIBC 2.15-0ubuntu10.4) 2.15 , gcc version 4.8.1/(Ubuntu EGLIBC 2.15-0ubuntu10.5~ppa1) 2.15)

-Wl,-u,pthread_cancel,-u,pthread_cond_broadcast,-u,pthread_cond_destroy,-u,pthread_cond_signal,-u,pthread_cond_wait,-u,pthread_create,-u,pthread_detach,-u,pthread_cond_signal,-u,pthread_equal,-u,pthread_join,-u,pthread_mutex_lock,-u,pthread_mutex_unlock,-u,pthread_once,-u,pthread_setcancelstate

1. reproduce the bug

g++ -g -O0 -static -std=c++11 t.cpp -lpthread
./a.out
terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)


nm a.out | egrep "\bpthread_.*"
                 w pthread_cond_broadcast
                 w pthread_cond_destroy
                 w pthread_cond_signal
                 w pthread_cond_wait
                 w pthread_create
                 w pthread_detach
                 w pthread_equal
                 w pthread_join
                 w pthread_mutex_lock
                 w pthread_mutex_unlock
                 w pthread_once
                 w pthread_setcancelstate

2. resolve the bug

g++ -g -O0 -static -std=c++11 t.cpp -lpthread -Wl,-u,pthread_join,-u,pthread_equal
./a.out  
FOO BAR  


nm a.out | egrep "\bpthread_.*"  
0000000000406320 T pthread_cancel
                 w pthread_cond_broadcast
                 w pthread_cond_destroy
                 w pthread_cond_signal
                 w pthread_cond_wait
0000000000404970 W pthread_create
                 w pthread_detach
00000000004033e0 T pthread_equal
00000000004061a0 T pthread_getspecific
0000000000403270 T pthread_join
0000000000406100 T pthread_key_create
0000000000406160 T pthread_key_delete
00000000004057b0 T pthread_mutex_lock
00000000004059c0 T pthread_mutex_trylock
0000000000406020 T pthread_mutex_unlock
00000000004063b0 T pthread_once
                 w pthread_setcancelstate
0000000000406220 T pthread_setspecific

这篇关于C ++ 0x线程静态链接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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