叮叮当当的包含优先级是怎么回事?



My command:

/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标志,clang告诉我:

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)

为什么 clang 包含来自 /opt/local/include 而不是 /usr/local/include ,尽管/usr/local/include在命令的-I目录中排在第一位?为什么/usr/local/include被推下优先级列表。

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

gcc -Wp,-v -E -

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

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

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

拥有两组具有相同名称的头文件是一个非常糟糕的主意(正如您发现的那样)。没有办法收拾那个烂摊子?

相关内容

  • 没有找到相关文章

最新更新