Keil µvision 5 头文件显示错误,但是编译没有问题
Keil µvision 5 header file shows an error, however it is compiled without problems
我可以访问这个项目。它是在 Keil µvision 5 中编译的。当我编译项目时,它没有错误。但是,当我访问头文件时,它显示一个错误,说 s8 变量有以下错误 = error: unknown type name 's8'.
typedef struct
{
s8 str[PARAM_TEXT_SIZE];
}
text_struct;
变量定义如下:
typedef char s8;
不知是我编译器配置错误,还是编译后忽略了这个错误
PS:这是我在 Whosebug 网站上的第一个问题。对不起,如果我的问题不清楚或放置错误。
如果 s8
是在头文件中定义的,比方说 foo.h
,而您的 text_struct
是在另一个头文件中定义的,则将其命名为 bar.h
那么它可以完美编译,如果包含 bar.h
的文件首先包含 foo.h
。然而它并不干净,通常一个好习惯是不要依赖这样的先决条件包括。
这是一个非常简单的例子:
foo.h
typedef int myType;
bar.h
typedef struct {
myType x;
} myStruct;
main.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "foo.h"
#include "bar.h"
myStruct y;
int main(void) {
return 0;
}
这将毫无问题地编译,但是如果对 bar.h
进行静态评估,则会产生错误,因为此处不知道 myType
。如果包含 bar.h
而没有包含 foo.h
,那么您将遇到编译错误。这是一个例子:
error.c
#include "bar.h"
myStruct z;
gcc -I. error.c -o error.o
In file included from error.c:1:0:
bar.h:2:5: error: unknown type name 'myType'
myType x;
我可以访问这个项目。它是在 Keil µvision 5 中编译的。当我编译项目时,它没有错误。但是,当我访问头文件时,它显示一个错误,说 s8 变量有以下错误 = error: unknown type name 's8'.
typedef struct
{
s8 str[PARAM_TEXT_SIZE];
}
text_struct;
变量定义如下:
typedef char s8;
不知是我编译器配置错误,还是编译后忽略了这个错误
PS:这是我在 Whosebug 网站上的第一个问题。对不起,如果我的问题不清楚或放置错误。
如果 s8
是在头文件中定义的,比方说 foo.h
,而您的 text_struct
是在另一个头文件中定义的,则将其命名为 bar.h
那么它可以完美编译,如果包含 bar.h
的文件首先包含 foo.h
。然而它并不干净,通常一个好习惯是不要依赖这样的先决条件包括。
这是一个非常简单的例子:
foo.h
typedef int myType;
bar.h
typedef struct {
myType x;
} myStruct;
main.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "foo.h"
#include "bar.h"
myStruct y;
int main(void) {
return 0;
}
这将毫无问题地编译,但是如果对 bar.h
进行静态评估,则会产生错误,因为此处不知道 myType
。如果包含 bar.h
而没有包含 foo.h
,那么您将遇到编译错误。这是一个例子:
error.c
#include "bar.h"
myStruct z;
gcc -I. error.c -o error.o
In file included from error.c:1:0: bar.h:2:5: error: unknown type name 'myType' myType x;