Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use clang++ instead g++ in Bazel

Tags:

bazel

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.
like image 772
Lanting Guo Avatar asked Dec 28 '16 05:12

Lanting Guo


People also ask

What compiler does Bazel use?

On Ubuntu, the default compiler is the first gcc compiler in the PATH . On macOS, the default compiler is the Apple LLVM compiler. To use Clang on Ubuntu, add --config=clang after any bazel build, bazel test or any other bazel commands.

Which C++ compiler does Bazel use?

Bazel still uses g++ as compiler. After an hour, Bazel uses clang++.

Which is faster GCC or Clang?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.

What Clang is used for?

The Clang tool is a front end compiler that is used to compile programming languages such as C++, C, Objective C++ and Objective C into machine code. Clang is also used as a compiler for frameworks like OpenMP, OpenCL, RenderScript, CUDA and HIP.


1 Answers

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.

like image 69
hlopko Avatar answered Oct 26 '22 19:10

hlopko