Have a strange problem with g++ compiled programs when using the "-static" switch.
I ran into this issue cross compiling executables for embedded ARM systems (though it seems architecture independent). Static linking all the libraries is required for deployment because the libraries are not on the target system. So I need the "-static" switch.
It seems C++ exceptions thrown in a std::thread are not being caught and cause the process to crash. I have narrowed it down to the code below:
#include <thread>
#include <iostream>
void thread_throwing_exception() {
try {
//this is a bad conversion that throws exception
long val = std::stoull("bad");
}
catch (const std::exception& ex) {
std::cerr << "caught exception: " << ex.what() << std::endl;
}
}
int main() {
std::thread thread;
thread = std::thread(thread_throwing_exception);
thread.join();
std::cout << "exiting\n";
return 0;
}
If I build it without the -static
switch:
g++- -std=c++11 test.cpp -o test_x64 -g -static-libstdc++ -static-libgcc -pthread
It executes as expected:
./test_x64
caught exception: stoull
exiting
Now with -static
switch. (I know it is not required for this program because there are no external libraries, but I need it for my application)
g++ -std=c++11 test.cpp -o test_x64 -g -static-libstdc++ -static-libgcc -pthread -static
Output:
./test_x64
Segmentation fault
Stack trace from gdb:
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff7ff9700 (LWP 13007)]
Thread 1 "test_x64" received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0 0x0000000000000000 in ?? ()
#1 0x0000000000401b27 in __gthread_equal (__t1=0, __t2=0) at /usr/include/x86_64-linux-gnu/c++/5/bits/gthr-default.h:680
#2 0x0000000000401eeb in std::operator== (__x=..., __y=...) at /usr/include/c++/5/thread:84
#3 0x0000000000401fb4 in std::thread::joinable (this=0x7fffffffde80) at /usr/include/c++/5/thread:170
#4 0x0000000000401f32 in std::thread::operator=(std::thread&&) (this=0x7fffffffde80,
__t=<unknown type in /media/sf_code/ziposoft/tests/test_bad/test_x64, CU 0x0, DIE 0x7a82>) at /usr/include/c++/5/thread:158
#5 0x0000000000401d28 in main () at test.cpp:33
I have tried g++-5, g++-7, g++-8. Any help would be greatly appreciated. From doing research, it seems "don't use static linking" is a popular sentiment, but for embedded deployment, static linking is very useful.
Aucun commentaire:
Enregistrer un commentaire