如何在Bazel中使用clang ++代替g ++ [英] how to use clang++ instead g++ in Bazel

查看:288
本文介绍了如何在Bazel中使用clang ++代替g ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用clang ++代替g ++来编译我的c ++文件,而g ++是系统的默认编译器.

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

我尝试了 sudo update-alternatives --install c ++ c ++/home/guo/bin/clang ++ 100 并设置了 CC 环境.但是它们不起作用.Bazel仍然使用g ++作为编译器.


一个小时后,Bazel使用clang ++.但是发生了错误.

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 是一个目录,而不是我计算机中的二进制文件.我猜这里应该是/home/guo/bin/clang ++ ,但是我不知道如何让 Bazel 知道.


附:似乎您在更改环境时需要重新启动 Bazel 服务器.

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.

推荐答案

要指定哪个C/C ++编译器,Bazel中的默认C ++工具链应使用set CC 环境变量(例如, CC =clang bazel build//......).

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

您可以使用-repo_env 选项,例如-repo_env = CC = clang ,以将此默认值放入您的项目或系统范围的 .bazelrc .

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

默认的Bazel C ++工具链使用系统安装的编译器,标头和库,而不尝试在BUILD文件中声明所有相关文件.这是为了简化用户的配置.因此,只要您以Bazel无法得知的方式修改C ++工具链(升级编译器的主要版本,将符号链接从gcc切换到clang,使用标头更改目录等),就必须运行 bazel clean--expunge 刷新缓存并在下一次重新运行自动配置.

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.

在Bazel中指定C ++工具链的可靠解决方案是使用CcToolchainConfigInfo.请参阅 https://docs.bazel.build/versions中的文档/master/tutorial/cc-toolchain-config.html

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.

这篇关于如何在Bazel中使用clang ++代替g ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆