库包含具有相同标头名称的路径

发布于 2024-10-20 11:44:06 字数 459 浏览 3 评论 0原文

将其他包含目录中的同名文件包含在另一个文件夹中的最佳方法是什么?

示例:

lib1/include/foo.h
lib2/include/foo.h

lib1/include 和 lib2/include 都添加到其他包含目录中。

编辑:

这些库来自不同的 SDK,每个开发人员都将它们安装在自己的位置。唯一可以确定的是,这两个文件夹都在 IDE 的附加包含路径中

方法 1:

#include "../../lib1/include/foo.h

方法 2:

在 lib2/ 之前添加 lib1/include包含在搜索路径中,因为它们是按顺序搜索的:

#include "foo.h"

lib1/include/foo.h 将被包含

What is the best method to include a file that has same name in another folder from additional include directories?

Example:

lib1/include/foo.h
lib2/include/foo.h

where both lib1/include and lib2/include are added in additional include directories.

Edit:

The libs are from different SDK's and every developer installs them in his own place. Only thing that is sure is that both folders are in IDE's additional include paths

method 1:

#include "../../lib1/include/foo.h

method2:

Add lib1/include before lib2/include in search paths and because they are searched in order with:

#include "foo.h"

lib1/include/foo.h will be included

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三生一梦 2024-10-27 11:44:06
#include "lib1/include/foo.h"
#include "lib2/include/foo.h"

只要这是这些标头的实际相对路径并且包含防护不同就可以了。例如,如果 foo.h 都使用

#ifndef _foo_h_

,那么这会给你一些你不想要的东西(它只包含一个,而不是两者,哪一个取决于执行顺序)。

#include "lib1/include/foo.h"
#include "lib2/include/foo.h"

Is fine as long as this is the actual relative path to those headers and the include guards are different. For example, if both foo.h use

#ifndef _foo_h_

then this will give you something you dont want (it will only include one, not both and which one depends on the order of execution).

尹雨沫 2024-10-27 11:44:06

您可以这样做:

#include "lib1/include/foo.h"
#include "lib2/include/foo.h"

并确保 lib1lib2 的父目录都在您的包含搜索路径中(但不是实际的 include 子目录本身)。

请注意,如果两个标头使用相同的符号作为包含保护,则此方法将不起作用 - 您需要取消定义两个包含之间的冲突符号:

#include "lib1/include/foo.h"
#undef FOO_H
#include "lib2/include/foo.h"

You can just do this:

#include "lib1/include/foo.h"
#include "lib2/include/foo.h"

and make sure that the parent directories of both lib1 and lib2 are in your search path for includes (but not the actual include subdirectories themselves).

Note that if both headers use the same symbol as an include guard then this method will not work - you would need to undefine the conflicting symbol between the two includes:

#include "lib1/include/foo.h"
#undef FOO_H
#include "lib2/include/foo.h"
谁对谁错谁最难过 2024-10-27 11:44:06

首先,这个答案假设两个标头的包含保护是兼容的(即不相同的符号)。

您可以做的一件事是在已知位置创建指向感兴趣的头文件的链接,并为链接本身指定不同的名称。例如,假设您的两个库安装在 $LIB1PATH 和 $LIB2PATH 中,它们在不同的构建环境中可能具有不同的值。因此,您想要获取的标头位于 $LIB1PATH/include/foo.h 和 $LIB2PATH/include/foo.h 。

你可以采取两种方式。一种是创建直接链接。在项目的目录树中,这可能看起来像这样:

$PROJDIR/
    include/
    lib_include/
        lib1_foo.h -> $LIB1PATH/include/foo.h
        lib2_foo.h -> $LIB2PATH/include/foo.h
    src/

如果您的代码位于存储库中,这可能会变得棘手,因为您无法签入这些链接;在其他环境中他们会错。另外,如果您有很多这些链接和很少的库,那么每当 lib1 或 lib2 移动时您都必须重新创建所有这些链接......这并不酷。您可以通过在包含项目目录的目录中创建链接来解决此问题:

$PROJDIR/
    include/
    lib_include/
        lib1_foo.h -> ../../lib1/include/foo.h
        lib2_foo.h -> ../../lib2/include/foo.h
    src/
lib1 -> $LIB1PATH/
lib2 -> $LIB2PATH/

在这两种情况下,您都需要确保 $PROJDIR/lib_include 位于包含路径中。另外,如果引入了两个 foo.h 标头,则只需在包含路径中包含 $LIB1PATH/include$LIB2PATH/include这些目录中的更多标头。您还可以将链接放入 include 中并删除 lib_include,但我喜欢将这些内容分开。

First off, this answer assumes that the include guards for the two headers are compatible (i.e. not the same symbols).

One thing you can do is create links in known locations to the header files of interest, giving the links themselves distinct names. For example, say your two libraries are installed at $LIB1PATH and $LIB2PATH, which could have different values in different build environments. Thus the headers you want to get are at $LIB1PATH/include/foo.h and $LIB2PATH/include/foo.h.

You could go two ways with this. One is by creating direct links. This could look like this in your project's directory tree:

$PROJDIR/
    include/
    lib_include/
        lib1_foo.h -> $LIB1PATH/include/foo.h
        lib2_foo.h -> $LIB2PATH/include/foo.h
    src/

This could get tricky if your code is in a repository, because you couldn't check these links in; they'd be wrong in other environments. Also, if you have a lot of these links and few libraries, you'd have to recreate all of them whenever lib1 or lib2 move... not cool. You can get around this problem by creating links in the directory that contains the project's directory:

$PROJDIR/
    include/
    lib_include/
        lib1_foo.h -> ../../lib1/include/foo.h
        lib2_foo.h -> ../../lib2/include/foo.h
    src/
lib1 -> $LIB1PATH/
lib2 -> $LIB2PATH/

In both cases, you need to make sure $PROJDIR/lib_include is on your include path. Also, you only need to have $LIB1PATH/include and $LIB2PATH/include in your include path if the two foo.h headers pull in more headers from those directories. You could also put the links in include and get rid of lib_include, but I like keeping these things separate.

天荒地未老 2024-10-27 11:44:06

我想知道目前有 4 个答案和 1709 个观点,但还没有人提出如此简单直接的解决方案。

您可以根据需要包含文件,您将遇到的唯一真正问题是相同的保护标头。因此,您必须创建一个标头,其中包含两个冲突的标头文件。我们将其命名为 inc_foo_lib1_lib2.h。在此文件中,您包含第一个 foo.h,然后如果定义了保护标头并取消定义它,接下来包含第二个 foo.h

例如,假设 foo.h 有一个标头保护 FOO_H_INCLUDED,那么您的 inc_foo_lib1_lib2.h 如下所示:

#include "lib1/foo.h"
#ifdef FOO_H_INCLUDED
#undef FOO_H_INCLUDED
#else
COMPILATION ERROR — the header guard was changed!
#endif //FOO_H_INCLUDED
#include "lib2/foo.h"

I wonder that with the 4 answers and 1709 views at the present moment nobody yet advised such an easy and straightforward solution.

You may include the files as you want, the only real problem that you're going to encounter is about the same guard headers. So, you have to create a header which will include both the conflicting header files. Let's call it inc_foo_lib1_lib2.h. In this file you include the first foo.h, then if the guard header was defined undefine it, next include the second foo.h.

As an example suppose that the foo.h have a header guard FOO_H_INCLUDED, then your inc_foo_lib1_lib2.h is looks like:

#include "lib1/foo.h"
#ifdef FOO_H_INCLUDED
#undef FOO_H_INCLUDED
#else
COMPILATION ERROR — the header guard was changed!
#endif //FOO_H_INCLUDED
#include "lib2/foo.h"
青衫儰鉨ミ守葔 2024-10-27 11:44:06

我将包含 -I 列表中明确目录中的文件(也就是说包含 lib1 和 lib2 的目录的路径):

#include "lib1/include/foo.h"
#include "lib2/include/foo.h"

因此不会有歧义,因为预编译器将查看其目录列表中的 lib1/include/foo.h ,而 lib1/include/lib2/include/ 中不会发生这种情况但只能来自父目录。

如前所述:请注意防护,即使对于 lib 标头,名称也应包含 lib 名称以避免此类混淆。

I would include the files from a unambiguous directory which is in the -I list (That is to say the path to the directory which contains lib1 and lib2):

#include "lib1/include/foo.h"
#include "lib2/include/foo.h"

Therefore there will be no ambiguity because the precompiler will look at lib1/include/foo.h within its directory list and that don't happen from lib1/include/ or lib2/include/ but only from the parent directory.

As said earlier: take care with the guards, even if for a lib header, the name should include the lib name to avoid such confusions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文