c#include指令中使用..(点点)是否合法?
作者:互联网
那里.正如标题所说.
使用#include“test.h”是合法的.
使用#include“test / test.h”也是合法的.
但使用#include“../ test.h”是否合法?
例如,#pragma once几乎在每个编译器中都是合法的.
但这不是C的标准.
我找不到任何说“保证”是父目录含义的文件.
谁能帮我?
解决方法:
C标准在§2.8/ 1中说:
Header name preprocessing tokens shall only appear within a
#include
preprocessing directive (16.2). The sequences in both forms of
header-names are mapped in an implementation-defined manner to headers
or to external source file names as specified in 16.2.
所以在这里你拥有它:它是所有实现定义的.然后§16.2说,在与你的问题没有直接关系的其他一些事情中:
A preprocessing directive of the form
#include
"
q-char-sequence"
new-linecauses the replacement of that directive by the entire contents of the
source file identified by the specified sequence between the"
delimiters. The named source file is searched for in an
implementation-defined manner.
这很有道理.为什么不能为不使用“..”约定的文件系统创建C编译器?
总而言之,回答你的问题:是的,根据C标准它是合法的,但这并不奇怪,因为几乎所有的东西在技术上都符合#include指令.
更有趣的问题是你的C实现的文档对此有何看法.但即便如此,您可能会对该主题留下相当通用的解释.例如,MSVC 2015 documentation for #include
说:
The path-spec is a file name that may optionally be preceded by a
directory specification. The file name must name an existing file. The
syntax of the path-spec depends on the operating system on which the
program is compiled.
标签:c,include,include-path 来源: https://codeday.me/bug/20190829/1760949.html