头文件中结构体互相引用的问题

Posted Magnum Programm Life

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了头文件中结构体互相引用的问题相关的知识,希望对你有一定的参考价值。

先上代码看下错误的例子:

typedef struct _thread{
    int       id;                        /* friendly id               */
    pthread_t pthread;                   /* pointer to actual thread  */
    thpool_handle_t thpool_p;            /* access to thpool          */
} thread_t;


/* Threadpool
 * threadpool has many threads, and he should to access each threads, threads pointer array to save all threads pointer */
typedef struct _thpool{
    thread_t**   threads;                  /* pointer to threads        */
    volatile int num_threads_alive;      /* threads currently alive   */
    volatile int num_threads_working;    /* threads currently working */
    pthread_mutex_t  thcount_lock;       /* used for thread count etc */
    jobqueue*  jobqueue_p;               /* pointer to the job queue  */
} thpool_t, *thpool_handle_t;

编译提示:
./include/thread_pool.h:31:5: error: unknown type name ‘thpool_handle_t’

 

修改如下解决:

struct _thread;
struct _thpool;
typedef struct _thread thread_t;
typedef struct _thpool thpool_t, *thpool_handle_t;

typedef struct _thread{
    int       id;                        /* friendly id               */
    pthread_t pthread;                   /* pointer to actual thread  */
    thpool_handle_t thpool_p;            /* access to thpool          */
} thread_t;


/* Threadpool
 * threadpool has many threads, and he should to access each threads, threads pointer array to save all threads pointer */
typedef struct _thpool{
    thread_t**   threads;                  /* pointer to threads        */
    volatile int num_threads_alive;      /* threads currently alive   */
    volatile int num_threads_working;    /* threads currently working */
    pthread_mutex_t  thcount_lock;       /* used for thread count etc */
    jobqueue*  jobqueue_p;               /* pointer to the job queue  */
} thpool_t, *thpool_handle_t;

 

以上是关于头文件中结构体互相引用的问题的主要内容,如果未能解决你的问题,请参考以下文章

结构体写在头文件中怎么写,在其它函数文件中怎么引用

qtcreator头文件中的结构体找不到

C语言头文件中定义结构体的问题

在C的头文件中定义的结构体,如何在cpp文件中引用

c语言一个结构体如何在多个源文件里面调用?

结构体的声明是在文件中的那个位置,函数的声明是在引用函数的声明部分还是在整个文件开始