#include 指令的正确格式
Correct format of #include directive
包含文件的 correct/recommended 方法是什么?我遇到了包含问题(VS2005 编译器...)。
我知道正确的方法是这样的:
#include "source.cpp"
或者这个:
#include <source.cpp>
# include "source.cpp"
会不会导致问题(在 # 后添加 space)?我的一些队友使用这种方式,我们遇到了一个未解决的问题。
我包含一个源文件的要点是我正在 IDE 下开发,它不允许在它的编辑器中定义函数,因为它们将成为本地函数。
回答您的问题:不,space 不会引起问题。在引入预处理指令的 #
和指令名称之间使用白色 space(space 和制表符)是完全合法的。 VS2005 足够现代,足以兑现这一点。
但是,非常 奇怪的是,您显然包含了一个 源文件 (.cpp
) 而不是只是一个头文件。虽然从技术上讲这没有错,但这很可能不是您真正想要做的。您没有指定收到的错误,但双重定义错误是典型的 class 由包含源文件(并单独编译)引起的错误。
#include "source.cpp"
# include "source.cpp"
那些是正确的(space 即使没有意义也不会导致任何问题)即使不推荐,请参阅此 post:Include .cpp file?
在某些时候,我已经在我的代码中包含了 .cpp 文件,以对其他代码进行某种静态导入。但这绝对不推荐,因为它会导致很多问题。例如,如果同一个 cpp 文件被包含两次,那么由该文件创建的 static
个对象也将被创建两次...所以它们不是 static
它们应该是的。此外,编译器可能会丢失,因为某些函数被定义了两次...
标准为预处理器指令 (16/4) 中的空格指定了以下规则:
The only white-space characters that shall appear between preprocessing tokens within a preprocessing
directive (from just after the introducing
#
preprocessing token through just before the terminating new-line
character) are space and horizontal-tab (including spaces that have replaced comments or possibly other
white-space characters in translation phase 3).
#
和 "filename"
/<filename>
都是预处理标记,所以它们之间可以有任意多的空格。
根据GCC手册中的C preprocessor documentation,可以在预处理指令中#
之后使用whitespace(例如#include
):
Preprocessing directives are lines in your program that start with
#
. The #
is followed by an identifier that is the directive name.
For example, #define
is the directive that defines a macro.
Whitespace is also allowed before and after the `#'.
因此 space 绝对不是您的问题,以下所有内容都是正确的:# include <file.h>
、#include <file.h>
、# include <file.cpp>
、#include <file.cpp>
, 虽然 你应该避免使用最后两个并且总是包含头文件.
话虽如此,我建议您不要在 #
之后使用任何白色 space - 这样您的代码将更具可读性,因为几乎使用 #include
一直以及有多少代码格式化程序会将您的代码格式化为无论如何。
C++ 标准在这方面也有同样的规定,请参阅:16.3 Macro replacement [cpp.replace]。
包含文件的 correct/recommended 方法是什么?我遇到了包含问题(VS2005 编译器...)。
我知道正确的方法是这样的:
#include "source.cpp"
或者这个:
#include <source.cpp>
# include "source.cpp"
会不会导致问题(在 # 后添加 space)?我的一些队友使用这种方式,我们遇到了一个未解决的问题。
我包含一个源文件的要点是我正在 IDE 下开发,它不允许在它的编辑器中定义函数,因为它们将成为本地函数。
回答您的问题:不,space 不会引起问题。在引入预处理指令的 #
和指令名称之间使用白色 space(space 和制表符)是完全合法的。 VS2005 足够现代,足以兑现这一点。
但是,非常 奇怪的是,您显然包含了一个 源文件 (.cpp
) 而不是只是一个头文件。虽然从技术上讲这没有错,但这很可能不是您真正想要做的。您没有指定收到的错误,但双重定义错误是典型的 class 由包含源文件(并单独编译)引起的错误。
#include "source.cpp"
# include "source.cpp"
那些是正确的(space 即使没有意义也不会导致任何问题)即使不推荐,请参阅此 post:Include .cpp file?
在某些时候,我已经在我的代码中包含了 .cpp 文件,以对其他代码进行某种静态导入。但这绝对不推荐,因为它会导致很多问题。例如,如果同一个 cpp 文件被包含两次,那么由该文件创建的 static
个对象也将被创建两次...所以它们不是 static
它们应该是的。此外,编译器可能会丢失,因为某些函数被定义了两次...
标准为预处理器指令 (16/4) 中的空格指定了以下规则:
The only white-space characters that shall appear between preprocessing tokens within a preprocessing directive (from just after the introducing # preprocessing token through just before the terminating new-line character) are space and horizontal-tab (including spaces that have replaced comments or possibly other white-space characters in translation phase 3).
#
和 "filename"
/<filename>
都是预处理标记,所以它们之间可以有任意多的空格。
根据GCC手册中的C preprocessor documentation,可以在预处理指令中#
之后使用whitespace(例如#include
):
Preprocessing directives are lines in your program that start with
#
. The#
is followed by an identifier that is the directive name. For example,#define
is the directive that defines a macro. Whitespace is also allowed before and after the `#'.
因此 space 绝对不是您的问题,以下所有内容都是正确的:# include <file.h>
、#include <file.h>
、# include <file.cpp>
、#include <file.cpp>
, 虽然 你应该避免使用最后两个并且总是包含头文件.
话虽如此,我建议您不要在 #
之后使用任何白色 space - 这样您的代码将更具可读性,因为几乎使用 #include
一直以及有多少代码格式化程序会将您的代码格式化为无论如何。
C++ 标准在这方面也有同样的规定,请参阅:16.3 Macro replacement [cpp.replace]。