如何让 g++ 搜索特定目录中的头文件?

编程入门 行业动态 更新时间:2024-10-27 14:25:45
本文介绍了如何让 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 的效果相同.-我/特殊/包括.

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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-28 05:48:09,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:头文件   目录中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!