[llvm-bugs] [Bug 52575] Fail to build LLVM due to cmath on macOS 10.13

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Nov 22 02:06:25 PST 2021


https://bugs.llvm.org/show_bug.cgi?id=52575

Dimitry Andric <dimitry at andric.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dimitry at andric.com
         Resolution|---                         |INVALID
             Status|NEW                         |RESOLVED

--- Comment #1 from Dimitry Andric <dimitry at andric.com> ---
This error occurs because you are compiling a C++ program, and are using
"-isystem
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include",
which puts this directory at the front of your include search dirs, *before*
the libc++ include directory.

This will not work correctly, and you should probably just stop adding
-isystem. But if that is not possible for other reasons, you should also add
the libc++ include directory *before* the regular include directory, i.e. do
something like:

-isystem ${SDKDIR}/usr/include/c++/v1 -isystem ${SDKDIR}/usr/include

(Assuming here that
SDKDIR="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk")

How this happens is as follows. By default, if you compile a C++ program, clang
sets up the include search dirs similar to:

#include <...> search starts here:
 /usr/local/include

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0/include

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks
(framework directory)

Any header such as <cmath> or <math.h> will therefore be found *first* in the
libc++ include directory, ${SDKDIR}/usr/include/c++/v1. Whenever libc++ wants
to use the C version of <math.h>, it will include it using
#include_next<math.h>, which attempts to find math.h from the *next* path in
the search directory list.

So normally, including <cmath> goes like:
* #include <cmath>       -> finds ${SDKDIR}/usr/include/c++/v1/cmath
* #include <math.h>      -> finds ${SDKDIR}/usr/include/c++/v1/math.h (this is
a wrapper header)
* #include_next <math.h> -> finds ${SDKDIR}/usr/include/math.h

Now if you add -isystem ${SDKDIR}/usr/include to your compilation, the include
search dirs will look like:

#include <...> search starts here:
 /usr/local/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/usr/include/c++/v1

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.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/System/Library/Frameworks
(framework directory)

I.e., ${SDKDIR}/usr/include is now before ${SDKDIR}/usr/include/c++/v1, and
then including <cmath> goes like:
* #include <cmath>       -> still finds ${SDKDIR}/usr/include/c++/v1/cmath, as
before
* #include <math.h>      -> finds ${SDKDIR}/usr/include/math.h, the plain C one
and *not* the wrapper header
* there won't be any #include_next <math.h> now since the plain C version does
not do that
* back in <cmath>, several "using" directives will now fail since the libc++
math.h wrapper was not included first

So the conclusion of this story is: if you really must set your own system
include paths using -isystem, that is fine, but you *must* also set the C++
include paths correctly, and in the right order, not only the plain C include
paths.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20211122/0231bf9a/attachment.html>


More information about the llvm-bugs mailing list