[英]header file include another header file generate redefinition
頭文件不能在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
以下是主要代碼和源鏈接 ,我認為這可能有助於理解我的問題。
#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
}
您不應多次包含Typedef.h,因為Typedef.h中沒有頭包含保護。
您的主要來源同時包含Typedef.h和EasyTransfer.h,這會導致重新定義錯誤,因為EasyTransfer.h也包含Typedef.h。 就像您的主要源代碼兩次包含Typedef.h一樣,而WITHOUT標頭包含后衛!
我建議您從主源文件中刪除#include“ Typedef.h”行。 或者,如果可以的話,在Typedef.h中添加標頭包含保護。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.