Skip to content

System headers not found when using clang with libc++ #13071

@SanjayVas

Description

@SanjayVas

Description of the problem / feature request:

Building C++ using clang and libc++ results in standard system headers not being found. For example:

In file included from external/com_google_protobuf/src/google/protobuf/compiler/
main.cc:31:                                                                     
external/com_google_protobuf/src/google/protobuf/compiler/cpp/cpp_generator.h:40:10: fatal error: 'string' file not found
#include <string>

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

  1. Install clang-11 and libc++ (e.g. via Debian packages clang-11, libc++-11-dev, libc++abi-dev)
  2. Create a cc_binary target with a C++ source file which includes a standard library system header.
  3. Create a .bazelrc file in the workspace root with options to enable C++ compilation with clang and libc++ (see below).
  4. Build the target using bazel build.

.bazelrc:

build --repo_env='CC=clang'
build --repo_env='BAZEL_CXXOPTS=-std=c++17:-stdlib=libc++'
build --repo_env='BAZEL_LINKLIBS=-lc++:-lm'

What operating system are you running Bazel on?

Linux (Debian Bullseye)

What's the output of bazel info release?

release 4.0.0

Have you found anything relevant by searching the web?

Yes. Relevant information on how to enable clang + libc++ as well as how auto-configure works for cc_toolchain_config.

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

I suspect the issue is with the combination of where clang lists its include path for libc++ headers and the the -no-canonical-prefixes compiler flag added by unix_cc_configure. For example, compare the include paths specified by clang using the following commands:

clang -E -xc++ - -v -stdlib=libc++ -std=c++17:

ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/llvm-11/bin/../include/c++/v1
 /usr/local/include
 /usr/lib/llvm-11/lib/clang/11.0.1/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.

clang -E -xc++ - -v -stdlib=libc++ -std=c++17 -no-canonical-prefixes:

ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/clang/11.0.1/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list

The path /usr/lib/llvm-11/bin/../include/c++/v1 which resolves to /usr/lib/llvm-11/include/c++/v1 is missing from the latter. Indeed, dropping -no-canonical-prefixes from the clang command that's shown when using the --sandbox_debug flag results in successful compilation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions