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开发者_JAVA百科 only used the standard library. I have been including things like <iostream>
and with no problems. 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:
#include <xercesc/sax2/SAX2XMLReader.hpp>
but g++ says "error: xercesc/sax2/SAX2XMLReader.hpp: No such file or directory". Where is it looking? Do I need to give it more information?
Thanks.
Use the --verbose
option:
[...]
#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
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
In your case, you could use CPLUS_INCLUDE_PATH
.
Gcc usually starts looking for include files in /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.
You might have to install the development package for Xerces to get the #include files.
The C++ Standard says in 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
The implementation-defined means that where and headers are searched and how headers location should be specified is specific to particular compiler. 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.
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. You can also use C_INCLUDE_PATH or CPLUS_INCLUDE_PATH environment variables.
Related question is How to add a default include path for gcc in linux?
To tell g++ where to look (apart from its defaults), you use the -I flag:
g++ -I/foo/bar xyz.cpp
tells it to look in the /foo/bar directory and construct paths from there. You can use multiple -I flags to specify multiple start points for the compiler to start looking.
On my rather old Windows system, Xerces is installed in /xerces, so I set up an include flag:
-I/xerces/include
Which allows me to say things like:
#include "sax2/SAX2XMLReader.hpp"
to include the file:
/xerces/include/sax2/SAX2XMLReader.hpp
The two forms of the #include directive are summarized fairly well by MSDN:
- 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. The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable.
- 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.
Also see this (duplicate/similar) question (for G++/GCC):
C++ #include semantics
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.
For the difference between header file and library, please check this post: What are Header Files and Library Files?
精彩评论