简体   繁体   English

如何在C和/或C ++中包含文件

[英]How to include a file in C and/or C++

I have a header file that I am trying to include from another source file using include pre-processor directory. 我有一个头文件,我试图包含使用include预处理器目录的另一个源文件。 I have tried to use both quoted form as well as angle-braket form, but neither seem to do the job. 我试图同时使用引用形式和角度形式,但似乎都没有做到这一点。

The file name is .>"hello.h and a directory where it is searched by the compiler. I have tried to include it like this: 文件名是.>"hello.h和编译器搜索它的目录。我试图像这样包含它:

  • #include <.>"hello.h>
  • #include <.\\>"hello.h>
  • #include <.\\>\\"hello.h>
  • #include ".>"hello.h"
  • #include ".>\\"hello.h"

I also tried different C and C++ compilers — clang, gcc, clang++ and g++. 我还尝试了不同的C和C ++编译器 - clang,gcc,clang ++和g ++。

Obviously, none of the above worked or otherwise there would have been no question. 显然,上述任何一项都不起作用,否则毫无疑问。

I thought that maybe the name is not legal according to the standard. 我认为根据标准,这个名字可能不合法。 Unfortunately, I have neither C nor C++ standard specifications on hand. 不幸的是,我手边没有C和C ++标准规范。 The only authoritative source of information I could find was this MSDN page about #include directive, and GNU C preprocessor documentation, here . 我能找到信息的唯一权威来源是该MSDN有关页面#include指令,以及GNU C预处理程序文件, 在这里 GNU's documentation does not say much, MSDN has the following clause, however: GNU的文档并没有多说,MSDN有以下条款,但是:

The path-spec is a file name optionally preceded by a directory specification. path-spec是一个文件名,可选地在目录规范之后。 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. path-spec的语法取决于编译程序的操作系统。

I am curious as to what C and C++ standards say about this? 我很好奇C和C ++标准对此有何看法?

Where do I find those OS-specific rules for C and C++ header file naming requirements? 在哪里可以找到针对C和C ++头文件命名要求的特定于操作系统的规则? I am particularly interested in OS X, Linux and FreeBSD. 我对OS X,Linux和FreeBSD特别感兴趣。

Why escaping < and/or " characters does not work? 为什么转义<和/或"字符不起作用?

How do I include my file? 我如何包含我的文件?

I think you are out of luck with that file name from the draft C99 standard section 6.4.7 Header names the grammar is as follows: 我认为你对draft C99 standard6.4.7 Header names节中的文件名6.4.7 Header names的语法如下:

header-name:
  < h-char-sequence >
  " q-char-sequence "
h-char-sequence:
  h-char
  h-char-sequence h-char
h-char:
    any member of the source character set except
    the new-line character and >
q-char-sequence:
  q-char
  q-char-sequence q-char
q-char:
    any member of the source character set except
    the new-line character and "

You have both a " and > in the file name which excludes you from both the q-char and h-char specification. I don't think you have much choice but to change the file name. 你同时拥有">中不包括你无论从文件名q-charh-char规范。我不认为你有很多选择,但要更改文件名。

The grammar is the same in the draft C++ standard , section 2.9 Header names . draft C++ standard2.9 Header names2.9 Header names的语法相同。

In both C and C++, that's not a valid header name since it contains both > and " . 在C和C ++中,这不是有效的头名,因为它包含>"

The syntax for header names allows those delimited by <> to contain "any member of the source character set except new-line and > ", and those delimited by "" to contain "any member of the source character set except new-line and " ". There is no concept of an escape sequence. 标题名称的语法允许由<>分隔的那些包含“除新行和>之外的源字符集的任何成员”,以及由""分隔的那些包含“除了换行符之外的源字符集的任何成员” " ”。没有逃脱序列的概念。

" and > are not valid characters for a filename in Windows. Your filename should be hello.h, or .\\hello.h, or ..\\hello.h, but not .>"hello.h. “和>不是Windows中文件名的有效字符。您的文件名应该是hello.h,或。\\ hello.h,或.. \\ hello.h,但不是。>”hello.h。

#include "hello.h"
#include ".\hello.h"
#include "..\hello.h"
#include "c:/temp/hello.h"

Which is why you will not find anything in MSDN about it. 这就是为什么你不会在MSDN中找到任何关于它的东西。

ext3 allows most characters (several have to be escaped when used), but it is HIGHLY recommended that you do not use them when naming your header and source files (if for no other reason than readability). ext3允许大多数字符(使用时必须转义几个字符),但强烈建议您在命名标题和源文件时不要使用它们(如果没有其他原因而不是可读性)。 For more information: http://pic.dhe.ibm.com/infocenter/compbg/v121v141/index.jsp?topic=%2Fcom.ibm.xlcpp121.bg.doc%2Flanguage_ref%2Fc99preprocessor.html 有关更多信息,请访问: http//pic.dhe.ibm.com/infocenter/compbg/v121v141/index.jsp?topic =%2Fcom.ibm.xlcpp121.bg.doc%2Flanguage_ref%2Fc99preprocessor.html

What is an acceptable filename is implementation defined. 什么是可接受的文件名是实现定义的。 I would have expected #include ".>\\"hello.h" to work, at least on systems where '>' and '"'` are legal in filenames, but there's no requirement that it work, and there are clearly systems where it won't, because such names are not legal in the system. 我希望#include ".>\\"hello.h" to work, at least on systems where '>' and '”'在文件名中是合法的系统上工作,但是没有要求它工作,并且有明确的系统它不会,因为这些名称在系统中是不合法的。

You might try forcing the issue: 您可以尝试强制解决此问题:

#define NAME ".>\"hello.h"
#include NAME

But for practical purposes, you should limit your filenames to alphanumerics, underscores, and maybe hyphens (but I'd be leary of those as well). 但是,出于实用的目的,你应该限制你的文件名以字母数字,下划线和连字符也许 (但我会猜疑这些的为好)。 And with only one dot, before the extension. 并且在扩展之前只有一个点。 If you go looking for trouble, don't be surprised if you find it. 如果你去寻找麻烦,如果你发现它,不要感到惊讶。

Which, of course, answers your last question: how do I include the file. 当然,这回答了您的上一个问题:如何包含该文件。 You rename it to something sensible. 你将它重命名为合理的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM