为什么 MSVC++如果我尝试将标头编译到不带换行符的包含文件中,会出现错误吗? (C++/Windows)
我正在使用 MSVC++ 来编程一个简单的窗口,并且我包含了一个菜单的资源
MYMENU MENU DISCARDABLE
//etc.
文件
#define ID_MYMENU_FILE_CLOSE 1002
:错误,但是,当我将其包含在资源文件中时,我收到错误
.\resourcedef.h(9) : fatal error RC1004: unexpected end of file found
(resourcedef.h 正好有 9 行长)。当我在 .h 的末尾添加换行符时,
//lines 1 - 8
#define ID_MYMENU_FILE_OPEN 1001
这样就有第十个空行(SO 上没有出现),它可以正常编译。如果我在第十行添加任何内容,即使是注释,编译器也会给出错误。有谁知道造成这种情况的原因以及如何解决它?
I'm using MSVC++ to program a simple window, and I included a resource file for a menu along the lines of:
MYMENU MENU DISCARDABLE
//etc.
and I created a header file "resourcedef.h" with definitions like
#define ID_MYMENU_FILE_CLOSE 1002
I can include it in my main.cpp file without error, however, when I include it in the resource file, I get the error
.\resourcedef.h(9) : fatal error RC1004: unexpected end of file found
(resourcedef.h is exactly 9 lines long). When I add a newline the the end of the .h,
//lines 1 - 8
#define ID_MYMENU_FILE_OPEN 1001
So that there is a tenth blank line (that doesn't appear on SO), it compiles fine. If I put anything on the tenth line, even a comment, the compiler gives me an error. Does anyone know what causes this and how I can fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
省略文本文件末尾的尾随换行符不是一个好主意 - 有许多工具除非它存在,否则将无法正常工作。 (有些文本编辑器甚至会警告您它丢失了。)
尾随换行符是文本文件的标准行为 - 为什么不顺其自然,只在那里放一个换行符。其他人都这样做。 8-)
It's a bad idea to omit the trailing newline at the end of a text file - there are many tools that will fail to work properly unless it's there. (Some text editors will even warn you that's it's missing.)
Trailing newlines are standard behaviour for text files - why not go with the flow and just put one there. Everybody else does. 8-)
很久以前,MSVC++ 版本 2 左右(1994 年左右)无法正确处理文件末尾的“部分”行。例如:
header.h
main.cpp
在这种情况下,预处理器在解析包含后将看到以下文本:
该错误是最后一行将如上所示一起运行,并且
"other.h" 文件不会被包含在内。使用当时的 IDE,很容易意外地创建一个没有尾随换行符的文件并遇到上述情况。
幸运的是,这个错误早已被修复。看起来您已经发现了一种情况,编译器警告它不会在行中间遇到文件结尾。这通常被认为是一件好事。
Long ago, MSVC++ at version 2 or so (1994-ish) would not handle "partial" lines at the end of the file properly. For example:
header.h
main.cpp
In this situation, the preprocessor would see the following text after resolving includes:
The bug was that the last line would run together as shown above, and the
"other.h"
file would not be included. With the IDE at that time, it was easy to accidentally create a file with no trailing newline and run into the above situation.Fortunately, this bug has long since been fixed. It looks like you've found one of the situations where the compiler warns that it doesn't expect to encounter the end of the file in the middle of a line. This is generally regarded as a good thing.