C ++ 0x static flow binding problem

I'm having trouble trying to statically link programs using C ++ 0x stream functions. The code looks like this: (The compiler is gcc 4.6.1 when testing 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; } 

I associate it with:

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

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

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

The result of debugging GDB is as follows:

 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 to static libstdC ++ could (possibly) make this error, and compiled C ++ 0x programs can run on systems without gcc 4.6 libs:

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

But first, we must make a symbolic link to "libstdc ++. A" in the current directory:

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

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

+10
c ++ multithreading c ++ 11
Aug 17 '11 at 9:30 a.m.
source share
4 answers

For reasons that I don’t know exactly (I think this is a mistake), you cannot use std :: thread on gcc 4.6 when linking statically, since the __ghtread_active_p () function will be turned on as the return value of false (look at the _M_start_thread assembly) by calling This is an exception. Perhaps they need weak characters for the pthread_create function and they are not there with the static binding, but why they don’t do it, otherwise it's outside of me (note that the assembly later contains things like callq 0x0 , it seems like something very not this way).

Currently, I personally use boost :: threads, since I use boost anyways ...

+6
Aug 17 '11 at 9:52
source share

You must make sure that you are contacting the pthread library, otherwise you will receive the message "Operation not allowed."

For example, to compile the source code, I would use the following:

 g++ -Wall -fexceptions -std=c++0x -g -c file.cpp -o file.o 

Then, contacting this:

 g++ -o file file.o -lpthread 

When you do this without object files, you can try something like this:

 g++ -Wall -fexceptions -std=c++0x -g main.cpp -o file -lpthread 

Remember to leave the libraries at the end, as they will only be used for the build process.

+6
Aug 08 2018-12-12T00:
source share

you can use -u to solve 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_dealach, -u, pthread_condalsign , pthread_join, -u, pthread_mutex_lock, -u, pthread_mutex_unlock, -u, pthread_once, -u, pthread_setcancelstate

1. reproduce the error

 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 error

 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 
+6
Jan 05 '15 at 6:58
source share

My previous answer was deleted and I wrote a detailed answer.

In normal cases, this problem occurs due to incomplete libpthread communication. I found information about this here https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590

You can try associating your application with the following flags:

 -Wl,--whole-archive -lpthread -Wl,--no-whole-archive 

You can also look at these questions with a similar problem: What are the correct link options for using std :: thread in GCC under linux? Running std :: thread with a static link causes a segmentation error

+2
Jul 07 '15 at 14:44
source share



All Articles