本人将多线程程序交叉编译后在板上执行,发现了一个让人费解的错误。
terminate called after throwing an instance of '__gnu_cxx::__concurrence_broadcast_error'
what(): __gnu_cxx::__concurrence_broadcast_error
查了一下出错原因,是arm工具链的pthread库没有正确复制到目标板,可通过使用arm-linux-g++ -Wl,--whole-archive -lpthread -Wl,--no-whole-archive -lc解决问题。
--whole-archive 和 --no-whole-archive 是ld(gnu连接器)专有的命令行参数,所以要添加-Wl才能使之辨认到该参数,
--whole-archive 可以把 在其后面出现的静态库包含的函数和变量输出到动态库,
所以使用 -Wl,--whole-archive -lpthread -Wl,--no-whole-archive -lc 选项可以将pthread库添加到可执行文件中,
这样子就解决了问题了。