简体   繁体   English

我的C ++编译器在哪里解析我的#includes?

[英]Where does my C++ compiler look to resolve my #includes?

this is a really basic question. 这是一个非常基本的问题。 I've been learning C++ and thus far I have only used the standard library. 我一直在学习C ++,到目前为止我只使用过标准库。 I have been including things like <iostream> and with no problems. 我已经包括像<iostream>这样的东西,没有任何问题。 Now I want to use Apache Xerces, so I've installed it on my machine (a Debian system) and am following a tutorial which says I need to include: 现在我想使用Apache Xerces,所以我已经将它安装在我的机器上(Debian系统),并且我正在按照一个教程说明我需要包括:

#include <xercesc/sax2/SAX2XMLReader.hpp>

but g++ says "error: xercesc/sax2/SAX2XMLReader.hpp: No such file or directory". 但是g ++说“错误:xercesc / sax2 / SAX2XMLReader.hpp:没有这样的文件或目录”。 Where is it looking? 在哪里看? Do I need to give it more information? 我需要提供更多信息吗?

Thanks. 谢谢。

Use the --verbose option: 使用--verbose选项:

[...]
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i686-pc-linux-gnu/4.4.2/../../../../include/c++/4.4.2
 /usr/lib/gcc/i686-pc-linux-gnu/4.4.2/../../../../include/c++/4.4.2/i686-pc-linux-gnu
 /usr/lib/gcc/i686-pc-linux-gnu/4.4.2/../../../../include/c++/4.4.2/backward
 /usr/local/include
 /usr/lib/gcc/i686-pc-linux-gnu/4.4.2/include
 /usr/lib/gcc/i686-pc-linux-gnu/4.4.2/include-fixed
 /usr/include
End of search list.
[...]

You can use the -I option to add search directories, as explained here: http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Directory-Options.html#Directory-Options 您可以使用-I选项添加搜索目录,如下所述: http//gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Directory-Options.html#Directory-Options

You can also use environment variables to change this permanently: http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Environment-Variables.html#Environment-Variables 您还可以使用环境变量永久更改此内容: http//gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Environment-Variables.html#Environment-Variables
In your case, you could use CPLUS_INCLUDE_PATH . 在您的情况下,您可以使用CPLUS_INCLUDE_PATH

Gcc usually starts looking for include files in /usr/include. Gcc通常开始在/ usr / include中查找包含文件。 If you have include files in other directories, you can add a -I option to the command line to tell the compiler to look there also. 如果在其他目录中包含文件,则可以在命令行中添加-I选项,以告诉编译器也可以查看。

You might have to install the development package for Xerces to get the #include files. 您可能必须安装Xerces的开发包才能获取#include文件。

The C++ Standard says in 16.2/2 C ++标准在16.2 / 2中说

A preprocessing directive of the form #include <h-char-sequence> new-line searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters #include <h-char-sequence> new-line形式的预处理指令搜索一系列实现定义的位置,以查找由<和>分隔符之间的指定序列唯一标识的头。

The implementation-defined means that where and headers are searched and how headers location should be specified is specific to particular compiler. 实现定义意味着搜索where和headers以及如何指定头位置特定于特定编译器。 In fact, it is possible implementations may not use a one header in one file convention, but some fancy packaging systems, for instance all a library is supposed to ship headers in .zip archive location of such archive is given to compiler, then compiler takes care of extracting headers from it, etc. 实际上,有可能实现可能不会在一个文件约定中使用一个头,但是一些花哨的包装系统,例如所有库都应该在.zip中存档头文件这样存档的位置被赋予编译器,然后编译器需要照顾从中提取标题等

What it means is that you are supposed to check documentation of compiler you are using for details about how to specify so called include directories , location of headers. 这意味着您应该检查您正在使用的编译器的文档,以获取有关如何指定所谓的包含目录 ,标头位置的详细信息。

In case of GCC compiler, use -I option - see Options for Directory Search in the manual for details. 对于GCC编译器,请使用-I选项 - 有关详细信息,请参阅手册中的“ 目录搜索选项” You can also use C_INCLUDE_PATH or CPLUS_INCLUDE_PATH environment variables. 您还可以使用C_INCLUDE_PATH或CPLUS_INCLUDE_PATH环境变量。

Related question is How to add a default include path for gcc in linux? 相关问题是如何在linux中为gcc添加默认包含路径?

To tell g++ where to look (apart from its defaults), you use the -I flag: 要告诉g ++要查看的位置(除了默认值),请使用-I标志:

g++ -I/foo/bar xyz.cpp

tells it to look in the /foo/bar directory and construct paths from there. 告诉它查看/ foo / bar目录并从那里构造路径。 You can use multiple -I flags to specify multiple start points for the compiler to start looking. 您可以使用多个-I标志来指定编译器开始查找的多个起始点。

On my rather old Windows system, Xerces is installed in /xerces, so I set up an include flag: 在我相当老的Windows系统上,Xerces安装在/ xerces中,所以我设置了一个include标志:

-I/xerces/include

Which allows me to say things like: 这让我可以说:

#include "sax2/SAX2XMLReader.hpp"

to include the file: 包括文件:

/xerces/include/sax2/SAX2XMLReader.hpp

In order to use a new library, only specifying the header file is not enough. 要使用新库,仅指定头文件是不够的。 You may also need to specify the related library defined in the header file by using -l[library name] and -L[library path] you want to be linked in your gcc commend. 您可能还需要使用-l [库名]和-L [库路径]指定要在gcc表达中链接的头文件中定义的相关库。

For the difference between header file and library, please check this post: What are Header Files and Library Files? 有关头文件和库之间的区别,请查看此帖子: 什么是头文件和库文件?

The two forms of the #include directive are summarized fairly well by MSDN : MSDN对这两种形式的#include指令进行了很好的总结:

  • Quoted Form: 引用形式:

This form instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of any files that include (#include) that file. 此表单指示预处理器在包含#include语句的文件的同一目录中查找包含文件,然后在包含(#include)该文件的任何文件的目录中查找。 The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable. 然后,预处理器沿/ I编译器选项指定的路径搜索,然后沿INCLUDE环境变量指定的路径搜索。

  • Angle-Bracket Form: 角度支架形式:

This form instructs the preprocessor to search for include files first along the path specified by the /I compiler option, then, when compiling from the command line, along the path specified by the INCLUDE environment variable. 此表单指示预处理器首先沿/ I编译器选项指定的路径搜索包含文件,然后在从命令行编译时,沿INCLUDE环境变量指定的路径搜索包含文件。

Also see this (duplicate/similar) question (for G++/GCC): 另请参阅此(重复/类似)问题(对于G ++ / GCC):

C++ #include semantics C ++ #include语义

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

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