错误未知的文件类型虽然它在头文件中声明并包含在内

时间:2014-08-15 10:42:13

标签: c gcc include makefile

我有一个我不明白的错误。如果我把include放在mail.c文件而不是mail.h文件中,我可以摆脱错误。我的代码如下所示:

mail.h文件:

#ifndef MAIL_H
#define MAIL_H
//Normal needed headers
#include <stdio.h>
#include <string.h>
#include <ctype.h>

//Custom headers
#include "pop3.h"
#include "pasw.h"
#include "rese.h"


#define NAME_SIZE 100 //Defines the length of e.g. the user name
#define PASSW_SIZE 100 //Defines the length of the password
#define ADD_SPACE 10 //Defines some additional space for arrays

#define HELPTEXT "help.txt" //Defines the name of the help.txt file

enum bool {true, false};
typedef enum bool bool;

#endif

pop3.h文件:

#ifndef POP3
#define POP3

// Open SSl headers
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

//Needed for the password input to not show the password
#include <termios.h>

//Normal needed headers
#include <stdio.h>
#include <string.h>
#include <ctype.h>

//Custom headers
#include "mail.h"
#include "pasw.h"
#include "rese.h"

void pop3(unsigned char *, unsigned char *, unsigned char *);
int GetUserPassw(unsigned char *, unsigned char *);
int login(BIO *, unsigned char *, unsigned char *, unsigned char *, bool, bool);
int GetUsername(unsigned char *;);
int GetInput(unsigned char*, int);
int GetNumberOfMessages(BIO *);
void RetrieveEmail(BIO *);

#endif

在邮件中。 c我已经包含了mail.h,而在pop3.c中我已经包含了pop3.h.我得到的错误是:

gcc -lcrypto -lssl -c mail.c 
In file included from mail.h:10:0,
             from mail.c:11:
pop3.h:24:69: error: unknown type name ‘bool’
 int login(BIO *, unsigned char *, unsigned char *, unsigned char *, bool, bool);
                                                                 ^
pop3.h:24:75: error: unknown type name ‘bool’
 int login(BIO *, unsigned char *, unsigned char *, unsigned char *, bool, bool);
                                                                       ^
Makefile:12: recipe for target 'mail.o' failed
 make: *** [mail.o] Error 1

我的Makefile看起来像这样:

CC=gcc
CFLAGS=-lcrypto -lssl -o
OFLAGS=-lcrypto -lssl -c
RM=rm -i

all: mail

mail:   mail.o pop3.o pasw.o rese.o
    $(CC) $(CFLAGS) mail mail.o pop3.o pasw.o rese.o

mail.o: mail.c
    $(CC) $(OFLAGS) mail.c 

pop3.o: pop3.c
    $(CC) $(OFLAGS) pop3.c

pasw.o: pasw.c
    $(CC) $(OFLAGS) pasw.c

rese.o: rese.c
    $(CC) $(OFLAGS) rese.c

clean:
    rm *.o mail

在我更改文件结构之前,它已经有效了。但是我可以通过使用include&#34;命令&#34;来摆脱这个问题。从mail.h到mail.c。

我期待着您的回复。

金问候, Greenality

3 个答案:

答案 0 :(得分:2)

首先阅读我的评论。其次,在包含依赖于类型的标题后,您可以定义类型bool

答案 1 :(得分:1)

pop3.h中,您在

的函数声明中使用了类型bool
int login(BIO *, unsigned char *, unsigned char *, unsigned char *, bool, bool);

但当pop3.h中包含mail.h时,bool尚未定义pop3.h,而mail.h包含pop3.h

由于您还在bool中包含#include,因此您在头文件之间存在循环依赖关系,因此您必须为类型pop3.h和{{创建自己的文件1}}它进入pop3.h标题。另一种方法是正确清理标头依赖关系,因为#include <openssl/bio.h>仅依赖于boolbool类型。

同样不是创建自己的布尔类型,而是使用来自stdbool.h的{​​{1}},pop3.h也可以正确转换,如果编译器提供它,则使用真正的布尔类型。

所以你的#ifndef POP3 #define POP3 #include <stdbool.h> // Open SSl headers #include <openssl/bio.h> void pop3(unsigned char *, unsigned char *, unsigned char *); int GetUserPassw(unsigned char *, unsigned char *); int login(BIO *, unsigned char *, unsigned char *, unsigned char *, bool, bool); int GetUsername(unsigned char *;); int GetInput(unsigned char*, int); int GetNumberOfMessages(BIO *); void RetrieveEmail(BIO *); #endif 最终会成为

pop3.c

虽然实现所需的缺少头文件将直接包含在#include <openssl/ssl.h> #include <openssl/err.h> #include <termios.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include "mail.h" #include "pasw.h" #include "rese.h" 文件中:

mail.h

我无法帮助mail.h,因为它缺少界面。您mail.c中的所有内容都应该在mail.c中,因为它与实施细节有关。 只有您希望为mail.h实施的用户提供的内容应该成为{{1}}中界面的一部分。

答案 2 :(得分:0)

在编译mail.c时,该文件首先包含mail.h的内容,而mail.h的内容又包含pop.h的内容,该内容试图包含mail.h但由于{{{}而导致文件中没有任何内容。 1}}已经定义。

由于在mail.h中定义 MAIL_H之前包含了,因此pop.h的内容不知道enum bool是什么。因此编译错误。在mail.h中包含pop.h之前,只需定义bool即可解决问题。