【问题标题】:Clang implicitly includes bunch of unwanted headersClang 隐式包含一堆不需要的标头
【发布时间】:2018-04-09 11:41:14
【问题描述】:

给定以下代码:

#include<iostream>

int main()
{

return 0;
}

当我尝试编译这个简单的代码时,clang 隐含地包含了一堆其他头文件,其中包含我明确想要的 iosteram

所以使用-M标志来写依赖:

$ clang main.cpp -M

输出是:

  /Library/Developer/CommandLineTools/usr/include/c++/v1/iostream \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__config \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/ios \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/iosfwd \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/wchar.h \
  /usr/include/wchar.h /usr/include/_types.h /usr/include/sys/_types.h \
  /usr/include/sys/cdefs.h /usr/include/sys/_symbol_aliasing.h \
  /usr/include/sys/_posix_availability.h /usr/include/machine/_types.h \
  /usr/include/i386/_types.h /usr/include/sys/_pthread/_pthread_types.h \
  /usr/include/Availability.h /usr/include/AvailabilityInternal.h \
  /usr/include/sys/_types/_null.h /usr/include/sys/_types/_size_t.h \
  /usr/include/sys/_types/_mbstate_t.h /usr/include/machine/types.h \
  /usr/include/i386/types.h /usr/include/sys/_types/_int8_t.h \
  /usr/include/sys/_types/_int16_t.h /usr/include/sys/_types/_int32_t.h \
  /usr/include/sys/_types/_int64_t.h /usr/include/sys/_types/_u_int8_t.h \
  /usr/include/sys/_types/_u_int16_t.h \
  /usr/include/sys/_types/_u_int32_t.h \
  /usr/include/sys/_types/_u_int64_t.h \
  /usr/include/sys/_types/_intptr_t.h \
  /usr/include/sys/_types/_uintptr_t.h \
  /usr/include/sys/_types/_ct_rune_t.h /usr/include/sys/_types/_rune_t.h \
  /usr/include/sys/_types/_wchar_t.h \
  /Library/Developer/CommandLineTools/usr/lib/clang/9.1.0/include/stdarg.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/stdio.h \
  /usr/include/stdio.h /usr/include/_stdio.h \
  /usr/include/sys/_types/_va_list.h /usr/include/sys/stdio.h \
  /usr/include/sys/_types/_off_t.h /usr/include/sys/_types/_ssize_t.h \
  /usr/include/time.h /usr/include/sys/_types/_clock_t.h \
  /usr/include/sys/_types/_time_t.h /usr/include/sys/_types/_timespec.h \
  /usr/include/_wctype.h /usr/include/__wctype.h \
  /usr/include/sys/_types/_wint_t.h /usr/include/_types/_wctype_t.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/ctype.h \
  /usr/include/ctype.h /usr/include/_ctype.h /usr/include/runetype.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__locale \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/string \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/string_view \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__string \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/initializer_list \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/cstddef \
  /Library/Developer/CommandLineTools/usr/lib/clang/9.1.0/include/stddef.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__nullptr \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/cstring \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/string.h \
  /usr/include/string.h /usr/include/sys/_types/_rsize_t.h \
  /usr/include/sys/_types/_errno_t.h /usr/include/strings.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/utility \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__tuple \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/cstdint \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/stdint.h \
  /Library/Developer/CommandLineTools/usr/lib/clang/9.1.0/include/stdint.h \
  /usr/include/stdint.h /usr/include/_types/_uint8_t.h \
  /usr/include/_types/_uint16_t.h /usr/include/_types/_uint32_t.h \
  /usr/include/_types/_uint64_t.h /usr/include/_types/_intmax_t.h \
  /usr/include/_types/_uintmax_t.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__debug \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/memory \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/typeinfo \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/exception \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/cstdlib \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/stdlib.h \
  /usr/include/stdlib.h /usr/include/sys/wait.h \
  /usr/include/sys/_types/_pid_t.h /usr/include/sys/_types/_id_t.h \
  /usr/include/sys/signal.h /usr/include/sys/appleapiopts.h \
  /usr/include/machine/signal.h /usr/include/i386/signal.h \
  /usr/include/machine/_mcontext.h /usr/include/i386/_mcontext.h \
  /usr/include/mach/machine/_structs.h /usr/include/mach/i386/_structs.h \
  /usr/include/sys/_pthread/_pthread_attr_t.h \
  /usr/include/sys/_types/_sigaltstack.h \
  /usr/include/sys/_types/_ucontext.h \
  /usr/include/sys/_types/_sigset_t.h /usr/include/sys/_types/_uid_t.h \
  /usr/include/sys/resource.h /usr/include/sys/_types/_timeval.h \
  /usr/include/machine/endian.h /usr/include/i386/endian.h \
  /usr/include/sys/_endian.h /usr/include/libkern/_OSByteOrder.h \
  /usr/include/libkern/i386/_OSByteOrder.h /usr/include/alloca.h \
  /usr/include/sys/_types/_dev_t.h /usr/include/sys/_types/_mode_t.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/new \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/limits \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__undef_macros \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/iterator \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__functional_base \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__functional_base_03 \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/tuple \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/stdexcept \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/cassert \
  /usr/include/assert.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/atomic \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/cstdio \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/cwchar \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/cwctype \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/cctype \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/wctype.h \
  /usr/include/wctype.h /usr/include/_types/_wctrans_t.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/mutex \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__mutex_base \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/chrono \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/ctime \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/ratio \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/climits \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/limits.h \
  /Library/Developer/CommandLineTools/usr/lib/clang/9.1.0/include/limits.h \
  /usr/include/limits.h /usr/include/machine/limits.h \
  /usr/include/i386/limits.h /usr/include/i386/_limits.h \
  /usr/include/sys/syslimits.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/system_error \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/cerrno \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/errno.h \
  /usr/include/errno.h /usr/include/sys/errno.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__threading_support \
  /usr/include/pthread.h /usr/include/pthread/pthread_impl.h \
  /usr/include/pthread/sched.h \
  /usr/include/sys/_pthread/_pthread_cond_t.h \
  /usr/include/sys/_pthread/_pthread_condattr_t.h \
  /usr/include/sys/_pthread/_pthread_key_t.h \
  /usr/include/sys/_pthread/_pthread_mutex_t.h \
  /usr/include/sys/_pthread/_pthread_mutexattr_t.h \
  /usr/include/sys/_pthread/_pthread_once_t.h \
  /usr/include/sys/_pthread/_pthread_rwlock_t.h \
  /usr/include/sys/_pthread/_pthread_rwlockattr_t.h \
  /usr/include/sys/_pthread/_pthread_t.h /usr/include/pthread/qos.h \
  /usr/include/sys/qos.h /usr/include/sys/_types/_mach_port_t.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/functional \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__functional_03 \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/locale.h \
  /usr/include/locale.h /usr/include/_locale.h /usr/include/xlocale.h \
  /usr/include/_xlocale.h /usr/include/xlocale/_ctype.h \
  /usr/include/xlocale/__wctype.h /usr/include/xlocale/_stdio.h \
  /usr/include/xlocale/_stdlib.h /usr/include/xlocale/_string.h \
  /usr/include/xlocale/_time.h /usr/include/xlocale/_wchar.h \
  /usr/include/xlocale/_wctype.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/streambuf \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/istream \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/ostream \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/locale \
  /usr/include/nl_types.h /usr/include/sys/types.h \
  /usr/include/sys/_types/_u_char.h /usr/include/sys/_types/_u_short.h \
  /usr/include/sys/_types/_u_int.h /usr/include/sys/_types/_caddr_t.h \
  /usr/include/sys/_types/_blkcnt_t.h \
  /usr/include/sys/_types/_blksize_t.h /usr/include/sys/_types/_gid_t.h \
  /usr/include/sys/_types/_in_addr_t.h \
  /usr/include/sys/_types/_in_port_t.h /usr/include/sys/_types/_ino_t.h \
  /usr/include/sys/_types/_ino64_t.h /usr/include/sys/_types/_key_t.h \
  /usr/include/sys/_types/_nlink_t.h \
  /usr/include/sys/_types/_useconds_t.h \
  /usr/include/sys/_types/_suseconds_t.h \
  /usr/include/sys/_types/_fd_def.h \
  /usr/include/sys/_types/_fd_setsize.h \
  /usr/include/sys/_types/_fd_set.h /usr/include/sys/_types/_fd_clr.h \
  /usr/include/sys/_types/_fd_zero.h /usr/include/sys/_types/_fd_isset.h \
  /usr/include/sys/_types/_fd_copy.h \
  /usr/include/sys/_types/_fsblkcnt_t.h \
  /usr/include/sys/_types/_fsfilcnt_t.h /usr/include/_types/_nl_item.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__bsd_locale_defaults.h \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/bitset \
  /Library/Developer/CommandLineTools/usr/include/c++/v1/__bit_reference

请注意,输出的第一行是我在 main.cpp 文件中明确包含在 #indlude 指令中的所需标头。 Rest header 是多余的。

我认为 clang 包含了这些标题,以便忘记包含必要标题的程序员。 但是如何禁用此行为,即如何告诉 clang 不要包含这些标头?

【问题讨论】:

  • 我敢打赌这些标头包含在 iostream 标头中。另外,您究竟为什么要这样做?你想解决什么问题?
  • 插件在 IDE 的 utlizis clang 中提供自动完成功能,以从标头中检索符号。它的结果是建议了很多多余的完成候选人。如果我可以禁用包含额外的headears,那么完成列表会更简洁。
  • 不幸的是,阻止这些包含会破坏编译,因此您无能为力(除了可能等待 C++20 模块)
  • &lt;iostream&gt;include at least 4 other headers 的标准要求。然后他们会拉进来他们需要的任何东西。
  • @BoPersson 我同意,但请看/usr/include/sys/_posix_availability.h/usr/include/sys/_types/_timeval.h 之类的headear。 不包括这些。有系统头文件,它们来自 C 语言。问题是在处理 IDE 的自动完成功能时。例如查看link的屏幕截图

标签: c++ macos clang macos-high-sierra


【解决方案1】:

iostream 包含或传递包含所有这些标头。

对于您想做的工作,我建议您查看 clangd:https://clang.llvm.org/extra/clangd.html

【讨论】:

    猜你喜欢
    • 2021-01-26
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2013-08-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多