Search code examples
ckeil

header file include another header file generate redefinition


Does the header file couldn't include another header file in C?

I download the code from Nuvoton website, for Keil C51 project, use the UART sample code, just add the file "EasyTransfer.h" and include "Typedef.h", the result shows lots of error message below.

\N79E85x_Sample_Code_V1.0.8(1)\Include\Typedef.h(1): error C231: 'BIT': redefinition \N79E85x_Sample_Code_V1.0.8(1)\Include\Typedef.h(2): error C231: 'UINT8': redefinition \N79E85x_Sample_Code_V1.0.8(1)\Include\Typedef.h(3): error C231: 'UINT16': redefinition \N79E85x_Sample_Code_V1.0.8(1)\Include\Typedef.h(4): error C141: syntax error near 'UINT32' \N79E85x_Sample_Code_V1.0.8(1)\Include\Typedef.h(6): error C231: 'uint8_t': redefinition \N79E85x_Sample_Code_V1.0.8(1)\Include\Typedef.h(7): error C231: 'uint16_t': redefinition \N79E85x_Sample_Code_V1.0.8(1)\Include\Typedef.h(8): error C141: syntax error near 'uint32_t'

The "EasyTransfer.h" is simple, just few of lines

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

The following is the main code and source link, I think it could be helpful to understand my question.

#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
}

Solution

  • You should not include Typedef.h multiple times since there is no header include guard in Typedef.h.

    Your main source includes Typedef.h and EasyTransfer.h at the same time, this causes redefinition errors because EasyTransfer.h includes Typedef.h too. Just like your main source includes Typedef.h twice, and WITHOUT header include guard!

    I suggest you just remove the #include "Typedef.h" line from your main source file. Or add header include guard in Typedef.h if you can.