未解析的符号 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*,其中 pthread_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() 调用中的强制转换会抑制警告,但将函数指针作为泛型 void* 传递是不好的形式,因为如果您要传递任何内容,它会阻止编译器警告您另一个是指向 to the create_task()`

的预期形式的函数指针