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

五迷三道 提交于 2021-02-06 15:16:05

问题


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.

回答1:


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.



来源:https://stackoverflow.com/questions/41356173/how-to-use-clang-instead-g-in-bazel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!