头文件包含另一个头文件生成重定义

时间:2016-08-23 02:52:15

标签: c keil

头文件是否在C中不包含另一个头文件?

我从Nuvoton网站下载代码,对于Keil C51项目,使用UART示例代码,只需添加文件“EasyTransfer.h”并包含“Typedef.h”,结果显示下面有很多错误信息。

\ N79E85x_Sample_Code_V1.0.8(1)\ Include \ Typedef.h(1):错误C231:'BIT':重新定义 \ N79E85x_Sample_Code_V1.0.8(1)\ Include \ Typedef.h(2):错误C231:'UINT8':重新定义 \ N79E85x_Sample_Code_V1.0.8(1)\ Include \ Typedef.h(3):错误C231:'UINT16':重新定义 \ N79E85x_Sample_Code_V1.0.8(1)\ Include \ Typedef.h(4):错误C141:'UINT32'附近的语法错误 \ N79E85x_Sample_Code_V1.0.8(1)\ Include \ Typedef.h(6):错误C231:'uint8_t':重新定义 \ N79E85x_Sample_Code_V1.0.8(1)\ Include \ Typedef.h(7):错误C231:'uint16_t':重新定义 \ N79E85x_Sample_Code_V1.0.8(1)\ Include \ Typedef.h(8):错误C141:'uint32_t'附近的语法错误

“EasyTransfer.h”很简单,只有几行

#ifndef EasyTransfer_h
#define EasyTransfer_h
#include "Typedef.h"
uint8_t * address;  //address of struct    
#endif

以下是主要代码和source link,我认为理解我的问题可能会有所帮助。

#define Uart_Port_Sel   0x00

#include <stdio.h>
#include "N79E85x.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "Version.h"
#include "EasyTransfer.h"
UINT8 u8Uart_Data;

//-----------------------------------------------------------------------------------------------------------
void main (void)
{
    AUXR1 |= Uart_Port_Sel;             // Select P10/P11 as UART pin(default)

    InitialUART0_Timer1(9600);          // 9600 Baud Rate @ 11.0592MHz
    Show_Version_Number_To_PC();
    ES = 1;                             // Enable serial interrupt
    EA = 1;                             // Enable global interrupt

    while(1);                           // Endless
}
//-----------------------------------------------------------------------------------------------------------
void UART_ISR(void) interrupt 4
{
    if (RI == 1)
    {                                   // If reception occur
        RI = 1;                         // Clear reception flag for next reception
        u8Uart_Data = SBUF;             // Read receive data
        SBUF = u8Uart_Data;             // Send back same data on UART
    }
    else TI = 0;                        // If emission occur
                                        // Clear emission flag for next emission
}

1 个答案:

答案 0 :(得分:0)

您不应多次包含Typedef.h,因为Typedef.h中没有包含guard的标头。

您的主要来源同时包含Typedef.h和EasyTransfer.h,这会导致重新定义错误,因为EasyTransfer.h也包含Typedef.h。就像你的主要来源包括Typedef.h两次,而WITHOUT标题包括guard!

我建议您从主源文件中删除#include“Typedef.h”行。或者如果可以的话,在Typedef.h中添加header include guard。