clang的include优先级是怎么回事? [英] What's going on with clang's include priorities?

查看:130
本文介绍了clang的include优先级是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的命令:

/usr/bin/c++ -fPIC -I/Users/me/project/include -I/usr/local/include/opencv \
-I/usr/local/include -I/opt/local/include -std=c++11 -O3 -M -c \
/Users/me/project/src/program.cpp | grep opencv

program.cpp 具有:

#include "opencv2/core/core.hpp"
#include "opencv2/ml/ml.hpp"

输出:

  /opt/local/include/opencv2/core/core.hpp \
  /opt/local/include/opencv2/core/types_c.h /usr/include/assert.h \
  /usr/include/math.h /opt/local/include/opencv2/core/version.hpp \
  /opt/local/include/opencv2/core/operations.hpp \
  /opt/local/include/opencv2/core/mat.hpp \
  /opt/local/include/opencv2/objdetect/objdetect.hpp \
  /opt/local/include/opencv2/ml/ml.hpp \

但是,存在:/usr/local/include/opencv2/core/core.hpp /usr/local/include/opencv2/ml/ml.hpp .

使用 -v 标志,c告诉我:

ignoring duplicate directory "/usr/local/include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 /Users/me/project/include
 /usr/local/include/opencv
 /opt/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)

尽管/usr/local/include ,为什么为什么从/opt/local/include 而不是/usr/local/include 包含clang>在命令的 -I 目录列表中排在首位吗?为什么/usr/local/include 下推优先级列表.

Why is clang including from /opt/local/include instead of /usr/local/include, despite /usr/local/include coming first in the list of -I directories in the command? Why is /usr/local/include pushed down the priority list.

推荐答案

您可以通过以下方式检查 #include 的默认搜索路径:

You can check the default search path for #include by:

gcc -Wp,-v -E -

(将 -v 标志提供给预处理器).

(give the -v flag to the preprocessor).

按照给您的目录顺序,在标准列表之前搜索您的目录(以 -I 给定).

Your directories (given with -I) are searched before the standard list, in the order you give them.

您明确给出了/usr/local/include ,而 gcc 忽略了您的指令,因为无论如何以后它都会被添加(作为系统目录);因此目录搜索顺序错误.如果您真的想控制自己搜索的目录,请使用 -nostdinc 并将其全部提供.那是非常脆弱的.

You give /usr/local/include explicitly, and gcc disregards your instruction because it will be added (as a system directory) later on anyway; thus the directories are searched in the wrong order. If you really want to control the directories searched yourself, use -nostdinc and give them all. That is extremely fragile.

拥有两组具有相同名称的头文件是一个非常糟糕的主意(如您所知).无法清除一团糟?

To have two sets of header files with the same names is a very bad idea (as you found out). No way to clean up that mess?

这篇关于clang的include优先级是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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