如何让g ++搜索特定目录中的头文件? [英] How to make g++ search for header files in a specific directory?

查看:32
本文介绍了如何让g ++搜索特定目录中的头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目(一个库),它被细分为几个目录,其中包含代码.我想让 g++ 在项目的根目录中搜索头文件,这样我就可以避免跨多个源文件的相同头文件的不同包含路径.

I have a project (a library) that is subdivided into a few directories with code in them. I'd like to to have g++ search for header files in the project's root directory, so I can avoid different include paths for same header files across multiple source files.

root/目录主要有子目录A/B/C/,里面都有 .hpp.cpp 文件.如果 A 中的某个源文件想要包含 B 中的 file.hpp,则必须这样做:#include "../B/file.hpp".C 中的另一个源文件也是如此.但是,如果 A 本身具有包含需要 file.hpp 的文件的子目录,那么如果我决定移动文件,它将不一致并且会导致错误(因为包含路径是 "../../B/file.hpp").

Mainly, the root/ directory has sub-directories A/, B/ and C/, all of which have .hpp and .cpp files inside. If some source file in A wanted to include file.hpp, which was in B, it would have to do it like this: #include "../B/file.hpp". Same for another source file that was in C. But, if A itself had sub-directories with files that needed file.hpp, then, it would be inconsistent and would cause errors if I decided to move files (because the include path would be "../../B/file.hpp").

此外,这也需要在其他项目中工作,这些项​​目位于 root/ 之外.我已经知道有一个选项可以手动将我的所有头文件复制到默认搜索目录中,但我想按照我描述的方式进行操作.

Also, this would need to work from other projects as well, which reside outside of root/. I already know that there is an option to manually copy all my header files into a default-search directory, but I'd like to do this the way I described.

所有使用该库的程序必须仅使用 g++ prog.cpp lib.a -o prog 进行编译.这意味着永久为 g++ 设置包含路径!

all programs using the library must compile only with g++ prog.cpp lib.a -o prog. That means permanently setting the include path for g++!

推荐答案

A/code.cpp

#include <B/file.hpp>

A/a/code2.cpp

A/a/code2.cpp

#include <B/file.hpp>

编译使用:

g++ -I /your/source/root /your/source/root/A/code.cpp
g++ -I /your/source/root /your/source/root/A/a/code2.cpp

您可以使用环境变量来更改 g++ 查找头文件的路径.从手册页:

You can use environment variables to change the path g++ looks for header files. From man page:

一些额外的环境变量会影响预处理器.

Some additional environments variables affect the behavior of the preprocessor.

   CPATH
   C_INCLUDE_PATH
   CPLUS_INCLUDE_PATH
   OBJC_INCLUDE_PATH

每个变量的值都是一个由特殊字符分隔的目录列表,很像 PATH,在其中查找标题文件.特殊字符PATH_SEPARATOR"取决于目标并在 GCC 构建时确定.对于基于 Microsoft Windows 的目标,它是一个分号,而对于几乎所有其他目标它都是一个冒号.

Each variable's value is a list of directories separated by a special character, much like PATH, in which to look for header files. The special character, "PATH_SEPARATOR", is target-dependent and determined at GCC build time. For Microsoft Windows-based targets it is a semicolon, and for almost all other targets it is a colon.

CPATH 指定要搜索的目录列表,就像使用 -I 指定一样,但在使用 -I 选项给出的任何路径之后命令行.这无论预处理哪种语言,都会使用环境变量.

CPATH specifies a list of directories to be searched as if specified with -I, but after any paths given with -I options on the command line. This environment variable is used regardless of which language is being preprocessed.

其余的环境变量仅在预处理指定的特定语言时适用.每个指定一个要成为的目录列表如同使用 -isystem 指定一样进行搜索,但在命令行上使用 -isystem 选项给出的任何路径之后.

The remaining environment variables apply only when preprocessing the particular language indicated. Each specifies a list of directories to be searched as if specified with -isystem, but after any paths given with -isystem options on the command line.

在所有这些变量中,一个空元素指示编译器搜索其当前工作目录.空元素可以出现在开头或路径的尽头.例如,如果 CPATH 的值为 ":/special/include",则与 -I 具有相同的效果.-I/特殊/包含.

In all these variables, an empty element instructs the compiler to search its current working directory. Empty elements can appear at the beginning or end of a path. For instance, if the value of CPATH is ":/special/include", that has the same effect as -I. -I/special/include.

您可以通过多种方式更改环境变量.在 bash 提示符下,您可以这样做:

There are many ways you can change an environment variable. On bash prompt you can do this:

$ export CPATH=/your/source/root
$ g++ /your/source/root/A/code.cpp
$ g++ /your/source/root/A/a/code2.cpp

您当然可以将其添加到您的 Makefile 等中.

You can of course add this in your Makefile etc.

这篇关于如何让g ++搜索特定目录中的头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆