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.
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.