库包含具有相同头名称的路径 [英] Library include paths with same header name

查看:169
本文介绍了库包含具有相同头名称的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其他文件夹中包含同名文件的最佳方法是什么?



示例:

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

其中lib1 / include和lib2 / include都添加在其他include目录中。



编辑:



这些库来自不同的SDK,每个开发人员都在自己的位置安装它们。只有确定两个文件夹都在IDE的其他包含路径中。



方法1:



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



method2:



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



#includefoo.h



将包含lib1 / include / foo.h

解决方案

首先,这个回答假设两个标题的包含警告是兼容的(即不是相同的符号)。



你可以做的一件事是在已知位置创建链接到感兴趣的头文件,给链接本身不同的名称。例如,你的两个库安装在$ 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 / 
/
lib_include /
lib1_foo.h - > ../../lib1/include/foo.h
lib2_foo.h - > ../../lib2/include/foo.h
src /
lib1 - > $ LIB1PATH /
lib2 - > $ LIB2PATH /

在这两种情况下,都需要确保 $ PROJDIR / lib_include 在您的包含路径上。此外,您只需要在包含路径中包含 $ LIB1PATH / include $ LIB2PATH / include code> foo.h headers从这些目录中导入更多的头文件。你也可以把链接放在 include 中,除去 lib_include ,但我喜欢把这些分开。 p>

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

解决方案

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.

这篇关于库包含具有相同头名称的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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