![](/img/trans.png)
[英]modlib3.c:21:9: error: invalid use of incomplete typedef 'DIR {aka struct _dirstream}
[英]Typedef C Struct: invalid use of incomplete typedef
尝试对结构进行 typedef 时出现以下错误。 我以前做过这件事,并且遵循与以前完全相同的格式,但有些东西不起作用,我完全被难住了。
shm_channel.h:
typedef struct _msgQ_info msgQ_info;
/*
* This function initializes and returns a mesQ_info struct for
* the user
*/
msgQ_info init_message_queue();
shm_channel.c:
// Struct that contains all the message queue information
struct _msgQ_info {
mqd_t descriptor;
mode_t mode;
char *name;
};
其他_file.c:
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <mqueue.h>
#include "shm_channel.h"
//... Inside of Main()
msgQ_info msgQinfo;
msgQinfo = init_message_queue();
if(0 > open_message_queue(&msgQinfo)){
fprintf(stderr, "message queue descriptor failed to be initialized in webproxy.c\n");
return 0;
}else{
fprintf(stderr, "Message queue descriptor successfully created with value : %d\n", msgQinfo.descriptor);
}
错误:
msgQ_info
是否旨在成为不透明类型? 如果是这样,您不应该从 Shm_channel.c 外部篡改它。
考虑一下这种设计的原因……你认为作者是否可能试图防止不可移植的内部结构泄漏到抽象之外并进入应该是可移植的代码?
如果您决定篡改它,您可能应该在 Shm_channel.c 的范围内这样做,其中结构(不可移植?)内部是隔离的。
在 Other_file.c 中将“shm_channel.h”替换为“shm_channel.c”
并且在shm_channel.c的开头有:
#include "shm_channel.h"
任何 .h 文件都应包含在具有相同(非限定)名称的 .c 文件中。
添加
#include "shm_channel.c"
前
#include "shm_channel.h"
在 Other_file.c 中
您的编译器将msgQ_info
视为“不完整的 typedef”,因为您没有告诉它struct _msgQ_info
是什么。 由于struct _msgQ_info
的声明已经存在于shm_channel.c
,你只需要#include
它。
或者,将声明添加到shm_channel.h
。
// Struct that contains all the message queue information
struct _msgQ_info {
mqd_t descriptor;
mode_t mode;
char *name;
};
typedef struct _msgQ_info msgQ_info;
/*
* This function initializes and returns a mesQ_info struct for
* the user
*/
msgQ_info init_message_queue();
我个人更喜欢第二种方法,因为它使您的项目更加清晰。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.