3

On my OSX it turns out /usr/local/include is always included as '-I', which caused a lot of headache for me:

hidden$ g++ -v -x c++ -isystem . -c /dev/null -o /dev/null
Apple clang version 11.0.0 (clang-1100.0.33.16)
Target: x86_64-apple-darwin19.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name null -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15 -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -ggnu-pubnames -target-linker-version 530 -v -coverage-notes-file /dev/null.gcno -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0 -isystem . -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I/usr/local/include -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1 -Wno-framework-include-private-from-public -Wno-atimport-in-framework-header -Wno-extra-semi-stmt -Wno-quoted-include-in-framework-header -fdeprecated-macro -fdebug-compilation-dir /tmp -ferror-limit 19 -fmessage-length 208 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.15.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /dev/null -x c++ /dev/null
clang -cc1 version 11.0.0 (clang-1100.0.33.16) default target x86_64-apple-darwin19.3.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 .
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.

The exact headache is not important here, but for anyone who are curious, my protobuf installed at /usr/local/include by Homebrew is used by bazel build, while I want to use my own project's version of protobuf. Bazel adds -isystem for the include paths, which is shown above that is shadowed by -I/usr/local/include.

I tried sudo xcode-select -s /Library/Developer/CommandLineTools but doesn't help.

Anyone knows where the -I/usr/local/include is ever introduced and how I can get rid of it (but not other standard include directories)? Is it some configuration that I can tweak, or is it hard-coded in compiler's binary?

Kan Li
  • 8,557
  • 8
  • 53
  • 93

1 Answers1

0

It's provided by Clang itself, by default.

You can supply the -nostdinc/--no-standard-includes to suppress this, but it also suppresses a bunch of other include directories.

A slightly more targeted way is by passing -Xclang -nostdsysteminc, but it still suppresses things you probably want. You can, of course, replace those manually on the command line.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • This is not working, by using `-Xclang -nostdsysteminc`, I only get `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1` and `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include`, which don't even have C headers like `stdio.h`. – Kan Li Mar 16 '20 at 22:13
  • Yes, like I said, you have to explicitly add back the include directories you want. You can see the ones you want/need in the output in your question. – Ken Thomases Mar 17 '20 at 07:29
  • thanks for your information, it is good to know, but I wouldn't use this approach because it is very hacky for my project to manually add those directories. I modified my question to clarify I only want to get rid of the `/usr/local/include` but not other standard locations. Furthermore, is this directory hard-coded in compiler's binary, or is it some configuration that I can tweak? – Kan Li Mar 17 '20 at 15:53
  • It's hard-coded. As near as I can tell, short of hacking the code and building the compiler yourself, there's no other way to avoid having it added. – Ken Thomases Mar 17 '20 at 18:35