embedded - 未解析的符号 pthread_create,首先在 ./armrtk/src/task.obj 中引用
问题描述
几天来我一直试图弄清楚这一点,但无法弄清楚。我正在使用 CCS 作为 IDE,并且正在使用 Windows。我正在尝试在 MSP432 上创建一个 RTOS 内核,并且需要使用 pthreads。我已经能够在其他示例中使用 pthreads,但我正在尝试编写自己的程序,并且在构建时遇到了这个问题:
unresolved symbol pthread_create, first referenced in ./armrtk/src/task.obj
我已将文件路径包含在 CCS 中,但我无法使用 .cfg 文件,因为我没有使用 XDCTools。我只需要这方面的帮助,我非常感谢。我也收到警告:
in pthread_create in TASK.C: #169-D argument of type "void *" is incompatible with parameter of type "void *(*)(void *)"
任务.H
#ifndef TASK_H
#define TASK_H
#include <pthread.h>
struct task_t {
pthread_t* thread;
int threadCheck;
int state;
};
void *task1(void);
void *task2(void);
struct task_t *create_task(void* functionptr);
void delete_task(void *task);
#endif
任务.C
#include <task.h>
#include <stdlib.h>
#include <pthread.h>
#define BLOCKED -1
#define READY 0
#define RUNNING 1
int testValue1 = 0;
int testValue2 = 0;
struct task_t *new_task;
pthread_t pntr;
struct task_t *create_task(void* functionptr) {
new_task = malloc(sizeof(struct task_t));
if(!new_task)
return NULL;
//set State of the new thread to ready
new_task->state = 0;
// check to see if pthread is created
**new_task->threadCheck = pthread_create(new_task->thread, NULL, functionptr, NULL);**
if(new_task->threadCheck!= 0){
//thread failed
return NULL;
}
return new_task;
}
void delete_task(void *task) {
if(task != NULL){
free(task);
pthread_exit(NULL);
}
}
解决方案
该unresolved symbol
错误是链接器错误,而不是编译器错误。您未能链接 pthreads 库。
关于警告functionptr
是void*
wherepthread_create()
需要一个带有签名的指向函数的指针void fn(void*)
。
您的任务函数在任何情况下都有不同的签名:void fn(void)
,因此在任何情况下,您都需要在调用中强制转换函数指针pthread_create()
(尽管通过省略参数,您失去了将信息传递到任务函数的有用方法void*
)。
修改task.h:
typedef void* (*task_t)(void);
struct task_t *create_task( task_t functionptr);
在task.cpp中
new_task->threadCheck = pthread_create( new_task->thread,
NULL,
(void (*)(void *))functionptr,
NULL ) ;
单独调用中的pthread_create()
强制转换会抑制警告,但是将函数指针作为泛型传递是不好的形式,因为如果您要将预期形式的函数指针以外的任何内容传递给create_task() void*
,它会阻止编译器警告您to the
`
推荐阅读
- javascript - 为什么我的数组没有填充到 HTTP 请求回调函数中?
- python - 为什么 tesseract 检测不到这个字母?
- azure - 基于客户 ID 的 Azure 服务总线队列分区
- set - 理解嵌套循环创建集合
- python - 从另一个 DataFrame 将列添加到 Pyspark DataFrame
- list - prolog 中的有效括号列表
- ubuntu - 在 ubuntu 20.04 中使用 make 命令时出错
- python - pip install gspread 安装到 python 而不是 py
- mysql - 将 MySQL 表行旋转到列
- php - 使此函数在数组上递归