Seg fault building with clang++ and -femulated-tls against gcc std lib

I am having a problem building on OSX with clang, against the gcc
standard library (there are project constraints behind why I am doing
this). I am using homebrew packages for both clang and gcc. This
pattern worked fine until a recent gcc package upgrade and I would at
least like to understand better what has happened.

Here's my example.cpp:
#include <mutex>
#include <iostream>
int main()
    std::once_flag flag;
    std::call_once(flag, [](){ std::cout << "Foo\n"; });
    return 0;

Now if I brew install llvm 7.1.0 and gcc 7.5.0, I can successfully
build as follows:

/usr/local/Cellar/llvm@7/7.1.0_1/bin/clang++ -isystem /usr/local/Cellar/gcc@7/7.5.0/include/c++/7.5.0 -isystem /usr/local/Cellar/gcc@7/7.5.0/include/c++/7.5.0/x86_64-apple-darwin18.7.0 -isystem /usr/local/Cellar/gcc@7/7.5.0/include/c++/7.5.0/backward -L /usr/local/Cellar/gcc@7/7.5.0/lib/gcc/7 -nostdinc++ -stdlib=libstdc++ -std=c++17 example.cpp -o example -femulated-tls

I have to add -femulated-tls, because we need to link to symbols such
as ___emutls_v._ZSt11__once_call in the gcc std lib (without this
option, it fails to link because it can't find _ZSt11__once_call).

However this fails at runtime:


Segmentation fault: 11 (core dumped)

I am not sure where to go next with this!

Some other notes:
* Building this example with gcc 7.5.0 against its own std lib works fine.
* Building this example against the std lib from gcc 7.4.0 (without
-femulated-tls) works fine, because that gcc version was built with
_GLIBCXX_HAVE_TLS not defined and therefore uses a different
implementation of std::once_call. I am still not sure if this change
in the gcc package is intentional (have asked on gcc mailing list).

Any help would be very much appreciated!


Update: I believe this is resolved by adding -lgcc_s.1 to the build
command. (gcc does this automatically)

So in case anyone else runs into this, here is my working build
command to build with clang on OSX against the gcc 7.5 std lib:

usr/local/Cellar/llvm@7/7.1.0_1/bin/clang++ -isystem
/usr/local/Cellar/gcc@7/7.5.0/include/c++/7.5.0 -isystem
-isystem /usr/local/Cellar/gcc@7/7.5.0/include/c++/7.5.0/backward -L
/usr/local/Cellar/gcc@7/7.5.0/lib/gcc/7 -nostdinc++ -stdlib=libstdc++
-std=c++17 -femulated-tls -lgcc_s.1 example.cpp -o example