首页 > 解决方案 > 未解析的符号 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);
    }
}

标签: embeddedkernelrtosmsp432

解决方案


unresolved symbol错误是链接器错误,而不是编译器错误。您未能链接 pthreads 库。

关于警告functionptrvoid*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 `


推荐阅读