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

查看:207
本文介绍了如何使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中的一些源文件想要包含 file.hpp ,它在B中,它必须这样做: #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

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 / special / include。

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天全站免登陆