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

时间:2022-11-29 02:06:28

I have a header file that I am trying to include from another source file using include pre-processor directory. I have tried to use both quoted form as well as angle-braket form, but neither seem to do the job.

我有一个头文件,我正试图从另一个源文件使用include预处理目录包含它。我试过同时使用两种引语形式和角舌形式,但这两种形式似乎都做不到。

The file name is .>"hello.h and a directory where it is searched by the compiler. I have tried to include it like this:

文件名是。>"你好。h和编译器搜索它的目录。我试着将它包括如下:

  • #include <.>"hello.h>
  • # include <。>“hello.h >
  • #include <.\>"hello.h>
  • # include <。\ >“hello.h >
  • #include <.\>\"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. The only authoritative source of information I could find was this MSDN page about #include directive, and GNU C preprocessor documentation, here. GNU's documentation does not say much, MSDN has the following clause, however:

我想也许这个名字不符合标准。不幸的是,我手头没有C或c++标准规范。我能找到的唯一权威信息来源是关于#include指令的MSDN页面,以及GNU C预处理器文档。GNU的文档没有说太多,MSDN有以下条款:

The path-spec is a file name optionally 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.

路径规范是一个可选的文件名,前面有目录规范。文件名必须命名一个现有的文件。路径规范的语法取决于编译程序的操作系统。

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? I am particularly interested in OS X, Linux and FreeBSD.

在哪里可以找到针对C和c++头文件命名需求的特定于操作系统的规则?我对OS X、Linux和FreeBSD特别感兴趣。

Why escaping < and/or " characters does not work?

为什么转义 <和 或> 字符不起作用?

How do I include my file?

如何包含我的文件?

4 个解决方案

#1


10  

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:

我认为你很不走运,因为C99标准条款草案6.4.7标题中的文件名语法如下:

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-char和h-char规范之外。我认为你除了更改文件名之外别无他法。

The grammar is the same in the draft C++ standard, section 2.9 Header names.

语法在草案c++标准中是相同的,第2.9节标题的名称。

#2


5  

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.

header名称的语法允许被<>分隔的那些包含“除new line和>之外的源字符集的任何成员”,以及那些由“”分隔的,以“”分隔的,以包含“除new line和“”之外的源字符集的任何成员”。没有转义序列的概念。

#3


2  

" 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). For more information: http://pic.dhe.ibm.com/infocenter/compbg/v121v141/index.jsp?topic=%2Fcom.ibm.xlcpp121.bg.doc%2Flanguage_ref%2Fc99preprocessor.html

ext3允许大多数字符(在使用时必须转义),但是强烈建议您在命名头文件和源文件时不要使用它们(如果没有其他原因的话,请不要使用它们)。更多信息:http://pic.dhe.ibm.com/infocenter/compbg/v121v141/index.jsp?topic=%2Fcom.ibm.xlcpp121.bg.doc%2Flanguage_ref%2Fc99preprocessor.html

#4


0  

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"要工作,至少在文件名中'>'和' ' ' '是合法的系统上是这样,但是没有要求它工作的要求,而且显然也有系统不会工作,因为这样的名字在系统中是不合法的。

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.

当然,这也回答了你的最后一个问题:如何包含文件?你把它重命名为合理的东西。

#1


10  

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:

我认为你很不走运,因为C99标准条款草案6.4.7标题中的文件名语法如下:

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-char和h-char规范之外。我认为你除了更改文件名之外别无他法。

The grammar is the same in the draft C++ standard, section 2.9 Header names.

语法在草案c++标准中是相同的,第2.9节标题的名称。

#2


5  

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.

header名称的语法允许被<>分隔的那些包含“除new line和>之外的源字符集的任何成员”,以及那些由“”分隔的,以“”分隔的,以包含“除new line和“”之外的源字符集的任何成员”。没有转义序列的概念。

#3


2  

" 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). For more information: http://pic.dhe.ibm.com/infocenter/compbg/v121v141/index.jsp?topic=%2Fcom.ibm.xlcpp121.bg.doc%2Flanguage_ref%2Fc99preprocessor.html

ext3允许大多数字符(在使用时必须转义),但是强烈建议您在命名头文件和源文件时不要使用它们(如果没有其他原因的话,请不要使用它们)。更多信息:http://pic.dhe.ibm.com/infocenter/compbg/v121v141/index.jsp?topic=%2Fcom.ibm.xlcpp121.bg.doc%2Flanguage_ref%2Fc99preprocessor.html

#4


0  

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"要工作,至少在文件名中'>'和' ' ' '是合法的系统上是这样,但是没有要求它工作的要求,而且显然也有系统不会工作,因为这样的名字在系统中是不合法的。

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.

当然,这也回答了你的最后一个问题:如何包含文件?你把它重命名为合理的东西。