首页 > 解决方案 > clang++ 不尊重 -isystem 标志,与 g++ 不同的行为

问题描述

在我的 macOS 系统上,我安装了以下内容:

当我查看 Google 的 Protobuf 存储库并使用 构建文件clang++时,它似乎忽略了-isystem我传递的标志:

git clone https://github.com/protocolbuffers/protobuf.git && cd protobuf && git checkout 326ea555b
clang++ -std=c++17 -isystem src -c src/google/protobuf/any_lite.cc

这给出了一个错误:

src/google/protobuf/any_lite.cc:56:19: error: return type of out-of-line definition of 'google::protobuf::internal::AnyMetadata::InternalPackFrom' differs from that in the declaration
bool AnyMetadata::InternalPackFrom(const MessageLite& message,
~~~~              ^
/usr/local/include/google/protobuf/any.h:108:8: note: previous declaration is here
  void InternalPackFrom(const MessageLite& message,
  ~~~~ ^
1 error generated.

发生这种情况是因为#include <google/protobuf/any.h>找到了文件/usr/local/include/google/protobuf/any.h。我希望它能够找到该文件src/google/protobuf/any.h,因为该文件存在并且我通过了-isystem srcInternalPackFrom自 3.14.0 版以来,私有函数的签名已更改,因此出现错误。

此外,当我尝试用 替换clang++g++-10,我得到了成功的构建。(我的印象是 Clang 努力争取与 GCC 的 flag-for-flag 兼容性):

git clone https://github.com/protocolbuffers/protobuf.git && cd protobuf && git checkout 326ea555b
g++-10 -std=c++17 -isystem src -c src/google/protobuf/any_lite.cc

为什么clang++忽略-isystem这里的标志?

标签: c++macosclang++include-path

解决方案


那不是因为-isystem src。出现问题是因为 clang++/usr/local/include在其标准包含目录列表中包含路径,而 g++ 没有。

您可以使用以下命令检查标准包含目录的列表:

clang++ -nostdlib -Wp,-v -E -

g++-10 -Wp,-v -E -

为避免/usr/local/include/google/protobuf/any.h被 clang++ 包含,您可以删除文件或使用-nostdinc-nostdinc++标志并手动传递旧的包含目录。


推荐阅读