2

Question

Can I custom the order of include path with clang 8?

For example:

Compile a project using clang with -v flag, it will output such logs.

#include <...> search starts here:

Path1

Path2

Path3

How can I move the Path3 ahead of Path2?

Detail about this problem is as follows.

Background

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.

The error is as follows (I omitted file paths for privacy reasons):

...xxxxx.mm:16:10: fatal error: 'fstream' file not found
#include <fstream>
         ^~~~~~~~~
1 error generated.

It seems like Clang cannot find C++ headers. (Not only stream but also other header files can't be found, too)

For comparing, I compiled the project with Apple Clang with -v flag, then it works fine. I compared the log outputs of these two version. Clang 8 didn't include

/Applications/Xcode11.7.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include

into its header search paths. Then I set HEADER_SEARCH_PATHS value with

/Applications/Xcode11.7.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1

I recompiled it, then I got another error:

In file included from <module-includes>: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 -> std -> Darwin
#include_next <ctype.h>

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:

Apple Clang (Compilation Successed)

#include <...> 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)

Clang 8 (Compilation Failed)

#include <...> 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)

If the ...XcodeDefault.xctoolchain/usr/include at the second-to-last position of this include path list, it would work.

So, how can I move it to the second-to-last position? Or there are some other problem with my compilation process?

  • Difficult to reproduce. Are you using [header guards](https://stackoverflow.com/a/4767103)? Are you accidentally using a C compiler to compile C++? – JHBonarius Dec 06 '20 at 08:41
  • @JHBonarius Thank you for your reply, for the first question, this error seems happed in C++ lib, and all my own header files are using header guards. For the second one, I am using clang++ to compile C++ code and clang for C code. PS. I am using Objective-C++. – r1cdjuabna1djauk1u Dec 06 '20 at 08:53

1 Answers1

3

I have solved this problem by:

export CPLUS_INCLUDE_PATH=/Applications/Xcode11.7.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1