Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
SanjayVas opened this issue Feb 20, 2021 · 2 comments
Closed

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

SanjayVas opened this issue Feb 20, 2021 · 2 comments
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-CPP Issues for C++ rules type: support / not a bug (process)

Comments

@SanjayVas
Copy link

SanjayVas commented Feb 20, 2021

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.

@fmeum
Copy link
Collaborator

fmeum commented Jul 9, 2021

I'm surprised to see this is not being tracked as a bug. Is a workaround known or is this viewed as a bug in clang? I would look into it and try to contribute a fix if I knew what the status is.

@fmeum
Copy link
Collaborator

fmeum commented Jul 12, 2021

The root cause of the issue is that Bazel uses -no-canonical-prefixes while calling clang through symlinks. I submitted a PR #13666 that fixes the issue for the reproducer in #13071 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-CPP Issues for C++ rules type: support / not a bug (process)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants