12

[Mac OS 10.13.2, Xcode9.2]

Clang has a flag -stdlib which, according to both clang++ -cc1 --help (same as clang -cc1 --help) and the LLVM documentation page, allows specification of the C++ standard library to use.

1) How does this flag impact on compilation? I.e. does it change the order of library include paths etc.

2) How does this flag impact linking? I.e. is it just a short-cut/alternative for supplying -lc++?

I am really interested in understanding the details of this flag because I can't find any documentation describing it's precise behaviour and it is causing havoc with our build system since the Xcode9 upgrade. Inclusion of -stdlib=libc++ in our Makefile causes the compilation to fail due to headers problems, yet, when -stdlib=libc++ is omitted, our projects compile fine (presumably because libc++ is the Mac OS default Standard C++ library). The project link against libc++ due to other linker flags -lc++ and -lsupc++.

Some background info about our use-case

We are using Clang to cross-compile to a -march=i686 -target i686-linux-elf target. Prior to the Xcode9 update, our build system was working fine. Since the upgrade we're getting compiler errors, such as:

/usr/local/our-target/sysroot/usr/local/include/c++/v1/stdlib.h:111:82: error: use of undeclared identifier 'labs'; did you mean 'abs'?
inline _LIBCPP_INLINE_VISIBILITY long      abs(     long __x) _NOEXCEPT {return  labs(__x);}

I've now been able to fix this problem by changing the header include paths. Namely I have removed a path reference to the folder that is parent to both the libc++ AND gcc4.8.5 includes.

#       -I${STAGING.nao}/usr/local/include/c++          \
        -I${STAGING.nao}/usr/local/include/c++/v1

I am still very interested in understanding the details of what the flag does.

gone
  • 1,079
  • 5
  • 13
  • 31
  • Maybe [this answer](https://stackoverflow.com/questions/19774778/when-is-it-necessary-to-use-use-the-flag-stdlib-libstdc) will help – Oliv Jan 08 '18 at 12:12
  • @Oliv: Thanks. I've already read that answer but it doesn't explain enough of the detail. However, it's reminded me that I could add some context to my question. – gone Jan 09 '18 at 01:07
  • 2
    So you have seen that this flag is used to specify which Standard Library implementation your code is going to be linked to: either *libstdc++* (the GCC implementation) or *libc++* (the LLVM project implementation). If on your plateform, Clang default to use *libc++* then the command option `-stdlib=libc++` will not have any effect. – Oliv Jan 09 '18 at 06:27
  • Obviously, if you change the standard library, as any library though, that change the include path (include/c++/ for libstdc++, include/c++/v for libc++). Apart from that, you can test by yourself which library it link against on your plateform by using `ldd` or an equivalent. (abi lib,unwind lib,...) – Oliv Jan 09 '18 at 06:50
  • Yes, I understand what you're saying. I've updated the OP to reflect that. The strange thing is that before Xcode9, the commented out include line did not cause problems. So it appears that there has been a subtle change in Clang5's behaviour. – gone Jan 09 '18 at 11:04
  • Certainly a mess with include order, and messy code: inside c++/v1/stdlib.h their is this "I am looking for trouble" line: `#include_next`. So if I were you, I would disable all automatic standard include and carefully define the order of include directory manualy!! – Oliv Jan 09 '18 at 12:00
  • I'll check that out. I've seen `#include_next` statements before but haven't absorbed their significance. My understanding is that the include order in our build system has already been carefully crafted. This is why it was working in Xcode8 and earlier. Something in Xcode9 has caused this problem, and it has to do with the `-stdlib` flag. – gone Jan 11 '18 at 10:01
  • Hum... specifying -I.../include/c++ and -I.../include/c++/v1 is at least weird. Why did you add these two lines? From my point of view, Xcode9 pointed out a mistake in the include directory list provided by your build system. – Oliv Jan 11 '18 at 13:23

0 Answers0