VS2019编译C代码报unexpected end of file found错误如何解决
问题根因与修复方案
核心错误点
你遇到的unexpected end of file found报错是预处理器解析失败导致的,直接原因有2个:
- test.h引用了C++专属头文件
<cstddef>,C语言编译器无法识别该头文件,解析中断 - 两个.c文件的#include语句使用了中文双引号
“test.h”,预处理器无法识别中文标点,无法正确定位头文件触发解析错误
另外你的代码还存在后续会触发编译错误的问题:
- C语言大小写敏感,test.c中结构体类型名定义为小写
students,函数实现时错写为大写Students - test.c中
anothernewfunction的返回值错写为大写Void,C语言关键字均为小写 - 函数名
new是C++保留关键字,即便C编译可通过,也建议改名避免后续兼容问题
修复步骤
- 修正test.h的头文件引用
将#include <cstddef>替换为C标准头文件:
#include <stddef.h>
- 替换所有#include后的中文双引号为英文半角双引号
" - 修正test.c的大小写错误:
Students*new()改为students* new()Void anothernewfunction()改为void anothernewfunction()
- 建议将
new函数重命名为new_students避免关键字冲突
修正后完整代码
test.h
//test.h #ifndef __test_h #define __test_h #include <stddef.h> typedef struct { float a; float b; }students; students* new_students(); void newfunction(); void anothernewfunction(); #endif
test.c
//test.c #include "test.h" students* new_students() { // 补充返回逻辑避免编译警告 return NULL; } void newfunction() {} void anothernewfunction() {}
testmain.c
//testmain.c #include "test.h" int main() { return 0; }
内容的提问来源于stack exchange,提问作者P Pp
相关产品推荐
相关产品推荐

