簡體   English   中英

C頭文件包含錯誤

[英]C header file include error

希望這是一個直截了當的問題......這是我重現這個問題的過程。 首先我創建我的源文件:

bash $ cat t.c
#include "t.h"

int main()
{
  ABC abc;
}

然后我創建相應的頭文件:

bash $ cat t.h
#ifdef _T_H
#define _T_H

#ifdef __cplusplus
extern "C" {
#endif

typedef struct abc { 
  int a;
} ABC;

#ifdef __cplusplus
}
#endif

#endif

然后,我嘗試編譯它:

bash $ gcc -o t t.c
t.c: In function ‘main’:
t.c:5: error: ‘ABC’ undeclared (first use in this function)
t.c:5: error: (Each undeclared identifier is reported only once
t.c:5: error: for each function it appears in.)
t.c:5: error: expected ‘;’ before ‘abc’

到底是怎么回事? 如果我使用'struct abc'而不是'ABC'作為tc中的類型,那么它會編譯。 為什么typedef不工作?

嘗試:

#ifndef _T_H
#define _T_H

我碰巧注意到這一點,因為_T_H沒有排隊,而我的潛意識大腦知道應該這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM