1

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.

  • That trace doesn't look like it's an exception not being caught. – T.C. Aug 02 '18 at 22:15
  • https://stackoverflow.com/questions/37492430/g-doesnt-resolve-pthread-once-used-in-stdlibc/37531869#37531869 – T.C. Aug 02 '18 at 22:17
  • You are correct. I got confused by another problem with my cross compiling that had the similar symptoms. I was missing this: -Wl,--whole-archive -lpthread -Wl,--no-whole-archive – Anthony Corriveau Aug 02 '18 at 22:56

1 Answers1

0

Answer is here. Actually had nothing to do with exceptions, I was confusing it with another issue I had.

when g++ static link pthread, cause Segmentation fault, why?