- 论坛徽章:
- 44
|
本帖最后由 windoze 于 2015-08-06 14:52 编辑
家里有一个小NAS盒子,用的是一个赛扬的嵌入式Linux系统,昨天心血来潮想给它写点代码,然后就掉进一个天坑里……
故事是这样的,这个鬼系统用的是64位内核,但绝大多数user land程序和库都是32位的,我本着不浪费的精神决定用64位程序,但系统中居然没有64位的libdl.so……好吧,反正我也不太需要动态库,我决定把程序静态链接。
地球人都知道glibc不支持静态链接,所以我就不浪费时间了,直接用musl,这个libc对静态链接支持良好。
重新build一个基于musl的libstdc++,没什么好说的,标准的cross compiling流程,也没遇到什么问题
编译链接我自己的程序,一切正常。
正当我以为万事大吉的时候,我忽然发现编译出来的程序不能跑,呐尼?
- terminate called after throwing an instance of 'std::system_error'
- what(): Enable multithreading to use std::thread: Operation not permitted
复制代码 为毛说不行泥?检查一下linker flag,发现没有-pthread,加上再试。
还是不行?试试-lpthread,还是不行?
用nm看看搞毛线?我一个静态链接的程序里面居然有未解决引用的符号?
看来问题找到了,pthread的函数没有链接进去,看来只能hack了:
- g++ ... -Wl,--whole-archive -lpthread ...
复制代码 还是不行?看来它是要逼着我发大招了,天马流……不对放错碟了……手动链接符号拳!
- g++ ... -lpthread -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 ...
复制代码 再试,嗯,好了,搞定收工。
改下代码不用std::thread,用boost::thread,一切正常,木有这种古怪问题。
|
|