库包含具有相同标头名称的路径
将其他包含目录中的同名文件包含在另一个文件夹中的最佳方法是什么?
示例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只要这是这些标头的实际相对路径并且包含防护不同就可以了。例如,如果 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
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).
您可以这样做:
并确保
lib1
和lib2
的父目录都在您的包含搜索路径中(但不是实际的include
子目录本身)。请注意,如果两个标头使用相同的符号作为包含保护,则此方法将不起作用 - 您需要取消定义两个包含之间的冲突符号:
You can just do this:
and make sure that the parent directories of both
lib1
andlib2
are in your search path for includes (but not the actualinclude
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:
首先,这个答案假设两个标头的包含保护是兼容的(即不相同的符号)。
您可以做的一件事是在已知位置创建指向感兴趣的头文件的链接,并为链接本身指定不同的名称。例如,假设您的两个库安装在 $LIB1PATH 和 $LIB2PATH 中,它们在不同的构建环境中可能具有不同的值。因此,您想要获取的标头位于 $LIB1PATH/include/foo.h 和 $LIB2PATH/include/foo.h 。
你可以采取两种方式。一种是创建直接链接。在项目的目录树中,这可能看起来像这样:
如果您的代码位于存储库中,这可能会变得棘手,因为您无法签入这些链接;在其他环境中他们会错。另外,如果您有很多这些链接和很少的库,那么每当 lib1 或 lib2 移动时您都必须重新创建所有这些链接......这并不酷。您可以通过在包含项目目录的目录中创建链接来解决此问题:
在这两种情况下,您都需要确保
$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:
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:
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 twofoo.h
headers pull in more headers from those directories. You could also put the links ininclude
and get rid oflib_include
, but I like keeping these things separate.我想知道目前有 4 个答案和 1709 个观点,但还没有人提出如此简单直接的解决方案。
您可以根据需要包含文件,您将遇到的唯一真正问题是相同的保护标头。因此,您必须创建一个标头,其中包含两个冲突的标头文件。我们将其命名为
inc_foo_lib1_lib2.h
。在此文件中,您包含第一个foo.h
,然后如果定义了保护标头并取消定义它,接下来包含第二个foo.h
。例如,假设
foo.h
有一个标头保护FOO_H_INCLUDED
,那么您的inc_foo_lib1_lib2.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 firstfoo.h
, then if the guard header was defined undefine it, next include the secondfoo.h
.As an example suppose that the
foo.h
have a header guardFOO_H_INCLUDED
, then yourinc_foo_lib1_lib2.h
is looks like:我将包含
-I
列表中明确目录中的文件(也就是说包含 lib1 和 lib2 的目录的路径):因此不会有歧义,因为预编译器将查看其目录列表中的
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):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 fromlib1/include/
orlib2/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.