title: How to customize include path order for clang?
body: <h2>Question</h2>
  <p>Can I custom the order of include path with clang 8?</p>
  <p>For example:</p>
  <p>Compile a project using clang with <code>-v</code> flag, it will output such logs.</p>
  <pre><code>#include &lt;...&gt; search starts here:
  
  Path1
  
  Path2
  
  Path3
  </code></pre>
  <p>How can I move the Path3 ahead of Path2?</p>
  <p>Detail about this problem is as follows.</p>
  <h2>Background</h2>
  <p>For some reason, I have to use clang v8.0 to compile my project with xcodebuild. Recently, I add some C++ code (in .mm files) for my project, then I got a fatal error when I build the code.</p>
  <p>The error is as follows (I omitted file paths for privacy reasons):</p>
  <pre><code>...xxxxx.mm:16:10: fatal error: 'fstream' file not found
  #include &lt;fstream&gt;
           ^~~~~~~~~
  1 error generated.
  </code></pre>
  <p>It seems like Clang cannot find C++ headers. (Not only stream but also other header files can't be found, too)</p>
  <p>For comparing, I compiled the project with Apple Clang with <code>-v</code> flag, then it works fine. I compared the log outputs of these two version. Clang 8 didn't include</p>
  <p><code>/Applications/Xcode11.7.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include</code></p>
  <p>into its header search paths. Then I set <code>HEADER_SEARCH_PATHS</code> value with</p>
  <p><code>/Applications/Xcode11.7.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1</code></p>
  <p>I recompiled it, then I got another error:</p>
  <pre><code>In file included from &lt;module-includes&gt;:1:
  /Applications/Xcode11.7.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ctype.h:38:15: fatal error: cyclic dependency in module 'Darwin':
        Darwin -&gt; std -&gt; Darwin
  #include_next &lt;ctype.h&gt;
  </code></pre>
  <p>This seems like the include path order caused a cyclic dependency in C++ header files. So I compared the output under apple clang with the output in this case. I discovered a tiny difference between their include path order:</p>
  <h3>Apple Clang （Compilation Successed）</h3>
  <pre><code>#include &lt;...&gt; search starts here:
  ...xxxxx.hmap (headermap)
  
  ...xxxxx.hmap (headermap)
   /Applications/Xcode11.7.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.3/include
   /Applications/Xcode11.7.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.7.sdk/usr/include
   /Applications/Xcode11.7.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
   /Applications/Xcode11.7.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.7.sdk/System/Library/Frameworks (framework directory)
  </code></pre>
  <h3>Clang 8 (Compilation Failed)</h3>
  <pre><code>#include &lt;...&gt; search starts here:
  ...xxxxx.hmap (headermap)
  
  ...xxxxx.hmap (headermap)
   
   /Applications/Xcode11.7.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
   ...xxx/clang/8.0.0/include
   /Applications/Xcode11.7.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.7.sdk/usr/include
   /Applications/Xcode11.7.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.7.sdk/System/Library/Frameworks (framework directory)
  </code></pre>
  <p>If the <code>...XcodeDefault.xctoolchain/usr/include</code> at the second-to-last position of this include path list, it would work.</p>
  <p>So, how can I move it to the second-to-last position? Or there are some other problem with my compilation process?</p>