简体   繁体   English

使用“-I”定义 g++ 包含路径:为什么它只在我设置“.../include”而不是“.../include/boost”时才有效?

[英]use “-I” to define g++ include path:why it only works when I set “…/include” instead of “…/include/boost”?

My application need boost lib.我的应用程序需要提升库。 There is a boost lib in /usr/lib and boost include in /usr/include/boost, but they aren's what I need. /usr/lib 中有一个 boost 库,/usr/include/boost 中有一个 boost 包含,但它们正是我所需要的。 So I compile new boost lib in my home, /home/js/anaconda/.../include/boost and /home/js/anaconda/.../lib.所以我在家里编译了新的 boost lib,/home/js/anaconda/.../include/boost 和 /home/js/anaconda/.../lib。

To use boost in home, I use "-I/home/js/anaconda/.../include/boost" to define the include path, however, It complain error because it find boost in the "/usr/..." path.要在家中使用 boost,我使用“-I/home/js/anaconda/.../include/boost”来定义包含路径,但是,它抱怨错误,因为它在“/usr/... “ 小路。 Then I try use "-I/home/js/anaconda/.../include" (the parent directory) and it works fine!然后我尝试使用“-I/home/js/anaconda/.../include”(父目录),它工作正常!

My question is 1)why it works when I specify the parent directory "/home/.../include" instead of "/home/.../include/boost"?我的问题是 1)为什么当我指定父目录“/home/.../include”而不是“/home/.../include/boost”时它会起作用? what is the right directory I should specify when I use "-I"?使用“-I”时应该指定的正确目录是什么?

2)when I use "-I" to specify some directory, will these directory always be the one prior to the /usr directory? 2)当我使用“-I”指定某个目录时,这些目录是否总是在 /usr 目录之前的目录?

It's a common practice (but not mandatory) to include all the header files of a library into its own directory.将库的所有 header 文件包含到其自己的目录中是一种常见做法(但不是强制性的)。 This has a few advantages:这有几个优点:

  • When you look for a header file, you don't need to look through the header files of other libraries.查找 header 文件时,无需查看其他库的 header 文件。
  • When you read a file with include directives, you can see all the headers that belong to the same library because they start in the same base directory.当您使用包含指令读取文件时,您可以看到属于同一个库的所有头文件,因为它们从同一个基目录开始。

When you use -I preprocessor flag to add a directory to the include search path, that directory can either:当您使用-I预处理器标志将目录添加到包含搜索路径时,该目录可以:

  • Belong to your current project: in the case you have your files organised in directories it may become handy.属于您当前的项目:如果您将文件组织在目录中,它可能会变得很方便。
  • Be a part of an external dependency: be it a system library or a dependency installed in a directory of your own.成为外部依赖项的一部分:无论是系统库还是安装在您自己的目录中的依赖项。 These directories' path end in include following the filesystem hierarchy standard .这些目录的路径以include结尾,遵循文件系统层次标准

In the particular case of boost, the intended use is to -I/path-to-boost-install/include flag and then use an include directive such as #include <boost/optional/optional.hpp> to use one of the libraries in that installation.在 boost 的特定情况下,预期用途是使用-I/path-to-boost-install/include标志,然后使用诸如#include <boost/optional/optional.hpp>之类的包含指令来使用其中一个库在那个安装中。

The best advice is to read the documentation and look for examples before you start using a library.最好的建议是在开始使用库之前阅读文档并查找示例。 In the case of boost, you can read the getting started on Unix or getting started on Windows pages:对于 boost,您可以阅读Unix 上的入门Windows 页面上的入门

It's important to note the following:请务必注意以下几点:

  • The path to the boost root directory (often /usr/local/boost_1_75_0) is sometimes referred to as $BOOST_ROOT in documentation and mailing lists. boost 根目录的路径(通常是 /usr/local/boost_1_75_0)在文档和邮件列表中有时称为 $BOOST_ROOT。

  • To compile anything in Boost, you need a directory containing the boost/ subdirectory in your #include path.要在 Boost 中编译任何东西,您需要一个目录,该目录在您的 #include 路径中包含boost/ 子目录。

  • Since all of Boost's header files have the.hpp extension, and live in the boost/ subdirectory of the boost root, your Boost #include directives will look like:由于 Boost 的所有 header 文件都具有 .hpp 扩展名,并且位于 boost 根目录的 boost/ 子目录中,因此您的 Boost #include 指令将如下所示:

 #include <boost/whatever.hpp>

or或者

 #include "boost/whatever.hpp"

depending on your preference regarding the use of angle bracket includes.取决于您对使用尖括号的偏好,包括。

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

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