【问题标题】:Error unkown filetype although it is declared in a header file and included错误未知的文件类型,尽管它在头文件中声明并包含
【发布时间】:2014-08-15 10:42:12
【问题描述】:

我有一个我不明白的错误。如果我只是将包含放在 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

在我稍微更改文件结构之前,它已经奏效了。但是我可以通过将包含“命令”从 mail.h 中放入 mail.c 中来解决这个问题。

期待收到您的来信。

国王的问候, 绿化

【问题讨论】:

  • 不要试图定义你自己的bool类型,而是包含&lt;stdbool.h&gt;
  • 这里没有“文件类型”。
  • 顺便说一句 CFLAGS OFLAGS
  • 非常感谢。 :-)

标签: c gcc include makefile


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    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 类型创建自己的文件,将其放入@987654332 @标头。另一种方法是正确清理您的标头依赖项,因为 pop3.h 仅依赖于 #include &lt;openssl/bio.h&gt;bool 类型。

    另外,不要创建您自己的布尔类型,而是使用来自stdbool.hbool,它与您的不同,它也可以正确转换,并且如果编译器提供它,则使用真正的布尔类型。

    所以你的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 中接口的一部分。

    【讨论】:

      【解决方案3】:

      在编译mail.c时,该文件首先包含mail.h的内容,该文件又包含pop.h的内容,pop.h试图包含mail.h,但由于@987654321而最终从文件中什么也得不到@ 已被定义。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-18
        • 2013-07-25
        • 2011-09-27
        • 2011-04-18
        • 1970-01-01
        • 2011-07-08
        相关资源
        最近更新 更多