Skip to content

C++ rules broken on macOS 12 Monterey and command line developer tools #14273

@nresare

Description

@nresare

Description of the problem / feature request:

There are mainly two ways to install the basic apple provided clang compiler toolchain on macOS. Either you install the Command Line developer tools by invoking xcode-select --install, or you install the full XCode development environment, which includes a graphical IDE and tooling for application development on all of Apple's operating systems. The latter is a significantly larger download at >10GiB.

Bazel 4.2.1 will detect and use the C++ compiler and linker that is available through the command line tools, but as of macOS 12 Monterey, attempting to build C++ binaries will succeed but the resulting binaries will fail on startup with a Segmentation Fault error. This is because the linker invocation generating the binary is not invoked with the -lc++ flag, indicating that the libc++ library should be loaded by the dynamic linker on startup.

The suggested solution to this problem is to install the full XCode IDE, which apparently works around this problem, but it would be neat to have this work out-of-the-box

Feature requests: what underlying problem are you trying to solve with this feature?

Make bazel more user friendly by having it build functional c++ binaries without non-obvious workarounds such as installing XCode on a pretty standard setup on one of the more common platforms.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

  1. On a vanilla macOS 12 Monterey machine, install the command line devloper tools with xcode-select --install
  2. download the latest stable bazel (I ran bazelisk that pulled bazel 4.2.1)
  3. Clone the bazelbuild/examples repository
  4. and build the hello world example:
    4.1 cd cpp-tutorial/stage1
    4.2 bazelisk build //main:hello-world
  5. Attempt to execute the resulting binary: ./bazel-bin/main/hello-world
% ./bazel-bin/main/hello-world
zsh: segmentation fault  ./bazel-bin/main/hello-world

What operating system are you running Bazel on?

I have reproduce this with macOS 12.0.1 Monterey on an Apple Silicon M1 MacBook as well as an Intel MacBook Pro.

% gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.9)

What's the output of bazel info release?

% bazelisk info release
release 4.2.1

What's the output of git remote get-url origin ; git rev-parse master ; git rev-parse HEAD ?

% git remote get-url origin ; git rev-parse main ; git rev-parse HEAD
https://github.com/bazelbuild/examples
67b1ee057fb532c8e7f15e202c0d8ccef4b507b9
67b1ee057fb532c8e7f15e202c0d8ccef4b507b9

Have you found anything relevant by searching the web?

There are a few github issues describing issues building bazel with bazel on macOS Monterey, and problems with segfaulting binaries, but they seem to be closed with the recommendation to install XCode, which seems to me like a workaround.

Any other information, logs, or outputs that you want to share?

Running bazel build --subcommands reveals that the required linker flag -lc++ is missing from the linker invocation. Adding linkopts = ["-lc++"], to the cc_binary rule will work around this problem.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions