20

I want to use clang++ instead g++ to compile my c++ files while g++ is the system's default compiler.

I have tried sudo update-alternatives --install c++ c++ /home/guo/bin/clang++ 100 and set CC environment. But they doesn't work. Bazel still uses g++ as compiler.


After an hour, Bazel uses clang++. But an error occurred.
ERROR: /home/guo/utils/lib/BUILD:2:1: C++ compilation of rule '//utils/lib:get_pdf' failed: linux-sandbox failed: error executing command /home/guo/.cache/bazel/_bazel_guo/d2d93a82f24e8dc5485ac1b29928428e/execroot/_bin/linux-sandbox ... (remaining 41 argument(s) skipped).
src/main/tools/linux-sandbox-pid1.cc:592: "execvp(/home/guo/lib/clang, 0x23abde0)": Permission denied
Target //utils/lib:get_pdf failed to buildenter code here
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.159s, Critical Path: 0.06s

Ps: /home/guo/lib/clang is a directory, not a binary in my computer. I guess here should be /home/guo/bin/clang++ but I don't know how to let Bazel know it.


Ps: It seems you need to restart Bazel server when you changed the environment.
Lanting Guo
  • 795
  • 3
  • 7
  • 21

1 Answers1

23

To specify which C/C++ compiler the default C++ toolchain in Bazel should use set CC environment variable (e.g. CC=clang bazel build //...).

You can use --repo_env option, e.g. --repo_env=CC=clang, to put this default into your project- or system-wide .bazelrc.

The default Bazel C++ toolchain is using system installed compiler, headers, and libraries without trying to declare all the related files in BUILD files. This is to simplify the configuration for the user. Therefore whenever you modify the C++ toolchain in a way that Bazel cannot know about (upgrade the major version of the compiler, switch symbolic links from gcc to clang, change directories with headers etc.), you have to run bazel clean --expunge to flush the cache and rerun the autoconfiguration the next time.

The robust solution to specifying C++ toolchain in Bazel is to use the CcToolchainConfigInfo. See the documentation at https://docs.bazel.build/versions/master/tutorial/cc-toolchain-config.html and https://docs.bazel.build/versions/master/cc-toolchain-config-reference.html.

hlopko
  • 3,100
  • 22
  • 27
  • Do you know where the CROSSTOOL files goes? What does the documentation mean by "top-level toolchain directory". I put it in the same place as my WORKSPACE file but it doesn't seem to be picking it up. – bantl23 May 06 '17 at 16:01
  • 1
    You should ask this as a separate question so it's easier to find for other people. Bazel is not searching for CROSSTOOL files anywhere, you must tell it where to look, e.g. by using --crosstool_top=... . Take a look here for more information: https://github.com/bazelbuild/bazel/wiki/Building-with-a-custom-toolchain – hlopko May 09 '17 at 16:28
  • The documentation is poor on how to make a CROSSTOOL file. I just want to use a different compiler, but crosstools contain many many other options that I don't know how to set appropriately. – Trevor Hickey Jun 27 '18 at 06:22
  • Then use CC environment variable, and BAZEL_LINKOPTS in addition if you need more control over libstdc++. – hlopko Jun 27 '18 at 07:18
  • here are 2 helpful repos for setting up toolchains: https://github.com/bazelbuild/bazel-toolchains https://github.com/grailbio/bazel-toolchain – Trevor Hickey Jan 06 '19 at 07:40
  • 2
    Could you post an example, please? I am willing to start a bounty for such an improvement. The documentation is really poor in this area. I even tried to use the `llvm_toolchain` you linked to to, but Bazel still invokes `gcc` – Victor Nov 08 '19 at 09:00