C/C++静态链接pthread库的坑以及分析解决
在嵌入式项目上使用pthread问题描述
最近在做嵌入式相关的项目,项目中使用到了pthread库,但特殊的是,并没有调用到pthread_create等接口,项目中仅仅只是调用了C++11标准的 std::call_once 接口。因为是嵌入式项目,所以需要静态编译移植到嵌入式Linux设备上。但很奇怪的是,不管我怎么编译,最终运行时都会报错:
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted
反复检查后确认了代码没有任何问题,这就让我很不解,而且也编译运行了动态链接版本,没有任何问题,编译、运行都正常。(需要说明的是,因为我是第一次做嵌入式项目,并没有太多嵌入式开发的经验,没有成功搭建起 GDB Server进行调试)但后来分析日志后发现是运行到存在 std::call_once 接口的函数体里崩溃的。为此我编写了一个demo进行确认问题:
#include <iostream>
#include <mutex>
using namespace std;
int main()
{
cout << "Hello world" << endl;
static once_flag of;
call_once(of,[]