C++ 静态链接c+时崩溃+;arm32上的螺纹

C++ 静态链接c+时崩溃+;arm32上的螺纹,c++,arm,C++,Arm,我已经在谷歌上搜索了很长一段时间了,但我没有看到任何像这样的东西,所以就这样。我正在尝试创建一个静态链接的二进制文件,它可以很容易地分布在我家庭网络上的机器上。这是一个相当小的项目,所以我尽量让事情简单 在ARM 32位体系结构上静态链接pthread库时,我遇到了很大的困难。令人沮丧的是,同样的代码在所有版本的x86上都可以正常工作。这是我的test.cpp程序: void threader( int num ) { std::cout << "Chil

我已经在谷歌上搜索了很长一段时间了,但我没有看到任何像这样的东西,所以就这样。我正在尝试创建一个静态链接的二进制文件,它可以很容易地分布在我家庭网络上的机器上。这是一个相当小的项目,所以我尽量让事情简单

在ARM 32位体系结构上静态链接pthread库时,我遇到了很大的困难。令人沮丧的是,同样的代码在所有版本的x86上都可以正常工作。这是我的test.cpp程序:

void threader( int num ) {

        std::cout << "Child Thread Starting" << std::endl;

        try {
                throw 20;
        } catch (int e) {
                std::cout << "Child Thread Success" << std::endl;
        }

        int x = 0;
        do {
                x++;
        } while (true);
}

int main(int argc, char *argv[]) {

        std::cout << "Main Thread Starting" << std::endl;

        new std::thread(&threader, 0);

        try {
                throw 20;
        } catch (int e) {
                std::cout << "Main Thread Success" << std::endl;
        }

        int y = 0;
        do {
                y++;
        } while (true);
}
然而,在ARM上,我会得到各种各样的错误,这取决于我如何准确地链接事物。作为参考,我从简单的开始:

$ g++ -std=c++11 -c -g test.cpp             
$ g++ -static *.o -o test -lrt -pthread
$ ./test
Main Thread Starting
Child Thread Starting
Main Thread Success
Segmentation fault
分段错误没有多大帮助:

(gdb) bt
#0  0x00012be8 in __cxa_throw ()
#1  0x000108d0 in threader (num=0) at test.cpp:11
#2  0x00011a26 in std::_Bind_simple<void (*(int))(int)>::_M_invoke<0u>(std::_Index_tuple<0u>) (this=0xda50c) at /usr/include/c++/4.8/functional:1732
#3  0x00011950 in std::_Bind_simple<void (*(int))(int)>::operator()() (this=0xda50c) at /usr/include/c++/4.8/functional:1720
#4  0x0001190a in std::thread::_Impl<std::_Bind_simple<void (*(int))(int)> >::_M_run() (this=0xda500) at /usr/include/c++/4.8/thread:115
#5  0x0001befc in execute_native_thread_routine ()
#6  0x0004bcc2 in start_thread (arg=0x0) at pthread_create.c:335
#7  0x00071b4c in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
嗯,它因与众不同而得到分数。GDB:

(gdb) r
Starting program: ./test 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".
Main Thread Starting
[New Thread 0x76ffc2d0 (LWP 30844)]
Child Thread Starting
terminate called after throwing an instance of 'int'
terminate called recursively

Thread 1 "test" received signal SIGABRT, Aborted.
0x00055266 in __libc_do_syscall ()
(gdb) bt
#0  0x00055266 in __libc_do_syscall ()
#1  0x0001ab66 in raise (sig=6) at ../sysdeps/unix/sysv/linux/pt-raise.c:35
#2  0x0005a85a in abort ()
#3  0x0004bd3c in __gnu_cxx::__verbose_terminate_handler() ()
#4  0x00026d34 in __cxxabiv1::__terminate(void (*)()) ()
#5  0x00026d50 in std::terminate() ()
#6  0x0001ca3c in __cxa_rethrow ()
#7  0x0004bd1c in __gnu_cxx::__verbose_terminate_handler() ()
#8  0x00026d34 in __cxxabiv1::__terminate(void (*)()) ()
#9  0x00026d50 in std::terminate() ()
#10 0x0001c9fc in __cxa_throw ()
#11 0x0001098e in main (argc=1, argv=0x7efff704) at test.cpp:28
(gdb) frame 11
#11 0x0001098e in main (argc=1, argv=0x7efff704) at test.cpp:28
28          throw 20;
(gdb)
有两件事需要注意:第一,现在是父线程正在崩溃,第二,显然是一个“int”正在出错。好啊那很特别

关于这一点,还有另一个答案,它似乎注意到了与此类似的东西:

但该解决方案建议围绕-lrt和-lpthread标志的顺序和频率进行调整。我试过。。。有很多这样的组合。它总是导致上述行为

我还将注意到,上述问题中的示例程序在有问题的ARM32系统上运行良好。但是,如果我将“throw20”测试块添加到线程函数中,它会立即中断

为了记录在案,我还尝试了许多与boost::thread的组合,还尝试了使用clang进行编译,都得到了相同的结果


在这一点上,我完全不知所措,沉溺于互联网的仁慈之中。有没有人知道这里发生了什么,或者我如何进行更多的调查?

可能没有关系,但请注意
do{}while(true)是严格未定义的。我扩展了空的do{}while循环,以便在它旋转时进行一些简单的计数。不幸的是,这并没有导致任何更改。我怀疑您还应该使用-pthread和link进行编译。使用-pthread可以设置其他选项,比如定义可重入。嘿,詹姆,你介意再给我说说你的建议吗?这是正确设置编译标志的问题,还是建议包含一些头文件?我尝试了几个明显的选项,但没有结果。@gf3dev您的所有对象文件都应该使用相同的“-pthread”选项编译。
$ g++ -std=c++11 -c -g test.cpp  
$ g++ -static *.o -o test -lrt -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive
$ ./test
Main Thread Starting
Child Thread Starting
terminate called after throwing an instance of 'Segmentation fault
(gdb) r
Starting program: ./test 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".
Main Thread Starting
[New Thread 0x76ffc2d0 (LWP 30844)]
Child Thread Starting
terminate called after throwing an instance of 'int'
terminate called recursively

Thread 1 "test" received signal SIGABRT, Aborted.
0x00055266 in __libc_do_syscall ()
(gdb) bt
#0  0x00055266 in __libc_do_syscall ()
#1  0x0001ab66 in raise (sig=6) at ../sysdeps/unix/sysv/linux/pt-raise.c:35
#2  0x0005a85a in abort ()
#3  0x0004bd3c in __gnu_cxx::__verbose_terminate_handler() ()
#4  0x00026d34 in __cxxabiv1::__terminate(void (*)()) ()
#5  0x00026d50 in std::terminate() ()
#6  0x0001ca3c in __cxa_rethrow ()
#7  0x0004bd1c in __gnu_cxx::__verbose_terminate_handler() ()
#8  0x00026d34 in __cxxabiv1::__terminate(void (*)()) ()
#9  0x00026d50 in std::terminate() ()
#10 0x0001c9fc in __cxa_throw ()
#11 0x0001098e in main (argc=1, argv=0x7efff704) at test.cpp:28
(gdb) frame 11
#11 0x0001098e in main (argc=1, argv=0x7efff704) at test.cpp:28
28          throw 20;
(gdb)