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 the include pre-processor directory. I tried to use both the quotation mark shape and the angular shape shape, but none of them do the job.

The file name .>"hello.h and the directory in which the compiler searches. I tried to include it like this:

  • #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 ++.

Obviously, none of the above actions were, or otherwise there would be no doubt.

I thought that perhaps the name is not legal according to the standard. Unfortunately, I have neither C nor standard C ++ specifications. The only authoritative source of information I could find was this MSDN page about the #include directive and the GNU C preprocessor documentation, here . The GNU documentation says little; MSDN has the following sentence:

A path specification is the name of a file that is not necessarily preceded by a specification directory. The file name must contain the name of the existing file. The syntax of the path specification depends on the operating system on which the program is compiled.

I am curious what is being said about this C and about C ++ standards?

Where can I find those OS-specific rules for C and C ++ header naming requirements? I'm particularly interested in OS X, Linux, and FreeBSD.

Why doesn't escaping < and / or " characters work?

How to include a file?

+8
c ++ c include c-preprocessor
source share
4 answers

I think you're out of luck with this file name from the draft C99 standard 6.4.7 Header names section: the grammar looks like this:

 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 " 

The file name has both " and > , which excludes you from the q-char and h-char specs. I don’t think you have much choice but to change the file name.

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

+10
source share

In C and C ++, this is not a valid header name, since it contains both > and " .

The syntax of the header names allows you to limit <> separator "any member of the original character set except for the new line and > ", and those that are limited to "" contain "any member of the source" character set except for the new line and " ". There is no concept of escape sequence.

+5
source share

"and> are invalid characters for the file name on Windows. Your file name must be hello.h or. \ hello.h or .. \ hello.h, but not.>" hello.h.

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

This is why you will not find anything on MSDN.

ext3 resolves most characters (some of which must be escaped when used), but it is strongly recommended that you do not use them when assigning the header and source files (unless for any 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

+2
source share

What is a valid file name, implementation is determined. I would expect that #include ".>\"hello.h" to work, at least on systems where '>' and '"' 'are legal in file names, but there is no requirement for it to work, and there are clear systems where this does not happen, because such names are not legal in the system.

You can try to fix the problem:

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

But for practical purposes, you should limit your file names to alphabetic characters, underscores, and maybe hyphens (but I would have the same identity). And with only one point, before the extension. If you are looking for problems, do not be surprised if you find it.

Which, of course, answers your last question: how to include a file. You will rename it something reasonable.

0
source share

All Articles