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

clang -std=c++11 does not find C++ headers #256360

Closed
attilaolah opened this issue Sep 20, 2023 · 4 comments
Closed

clang -std=c++11 does not find C++ headers #256360

attilaolah opened this issue Sep 20, 2023 · 4 comments
Labels
0.kind: bug Something is broken 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related

Comments

@attilaolah
Copy link
Contributor

Describe the bug

Given an example hello_world.cc file like this:

#include <iostream>

auto main() -> int {
    std::cout << "Hello, World!" << std::endl;
    return EXIT_SUCCESS;
}

clang fails to compile it with the command:

clang -std=c++11 -c hello_world.cc

The clang installed by the underlying system (Arch) has no trouble compiling this file.

And yes, there is clang++ for C++ files, but Bazel only invokes clang (or CC for that matter), and running Bazel with CC=clang seems to fail whenever it encounters a C++ compilation unit that uses the standard library, which is pretty much all of them.

Steps To Reproduce

  1. Create the hello_world.cc from above.
  2. Run clang -std=c++11 -c hello_world.cc

Expected behavior

There should be no errors and a 0 exit status, with a hello_world.o file generated.

Additional context

I have Arch linux as my base system which runs the Nix systemd service, with clang installed via home-manager.

Notify maintainers

Metadata

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
this path will be fetched (0.00 MiB download, 0.00 MiB unpacked):
  /nix/store/bysc0bn22cdwy1wzrn0n3rdid07d9cla-nix-info
copying path '/nix/store/bysc0bn22cdwy1wzrn0n3rdid07d9cla-nix-info' from 'https://cache.nixos.org'...
 - system: `"x86_64-linux"`
 - host os: `Linux 6.5.3-arch1-1, Arch Linux, noversion, rolling`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.17.0`
 - nixpkgs: `/home/atl/.nix-defexpr/channels/nixpkgs`
@attilaolah attilaolah added the 0.kind: bug Something is broken label Sep 20, 2023
@attilaolah
Copy link
Contributor Author

Still not sure whether or not this is working as intended, but it seems that explicitly passing something like -isystem /nix/store/nrc9pw6a2kf5rm6indqyj4yvk3bb6wwb-libcxx-11.1.0-dev/include/c++/v1 to clang solves the issue.

It still doesn't solve the issue with Bazel's auto-configured toolchain, since it would not recognise the /nix/store/… path as a system include path and wouldn't add it, not even as a user-defined --cxxopt since it is outside the execution root.

@attilaolah
Copy link
Contributor Author

Solved by adding the following line to my .bazelrc:

build --cxxopt=-xc++ --host_cxxopt=-xc++

Not sure why this is required by the flavour of clang shipped with Bazel, maybe some version incompatibility since Nix still gives me version 11, while the one on the host OS is version 16.

Anyway, since I'm not sure whether this is a bug or working as intended, I'll keep it open, but since my problem is now solved, feel free to close this if this is expected behaviour.

@Artturin
Copy link
Member

Artturin commented Sep 21, 2023

You're supposed to compile c++ files with clang++ right?

tests.cc-wrapper.llvmTests.llvmPackages_11.clang fails if I change the $CXX to $CC

$CXX -o cxx-check ${./cxx-main.cc}

so it should be working as intended

EDIT: I see that you've already tried that but that should be a bazel bug because every other build system uses the compilers correctly.

Same issue #150655

@attilaolah
Copy link
Contributor Author

Ah sorry I didn't find #150655. Let me close this one as duplicate then. I'm not sure I agree with the fact that this is a Bazel bug, since arguably Bazel uses the compilers "correctly", except it doesn't use the provided wrappers that automatically add the C++ header files, which is ultimately down to flexibility since one might want to use a different standard library (or ship their own). But that's besides the point.

Eventually I got things to compile using Clang $CC with the right C++ flags though, but I can't find the right incarnation now. I'll close this bug and update it if I find the right set of commands.

@rrbutani rrbutani added the 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related label May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: bug Something is broken 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related
Projects
None yet
Development

No branches or pull requests

3 participants