【发布时间】:2022-01-20 16:30:48
【问题描述】:
我试图在使用 bazel 构建时传递编译器参数。我想禁用 Bazel 的所有优化来构建一个简单的项目。实际上,我设法在没有 bazel 的情况下使用带有这段代码的 windows 命令行来做到这一点:
没有挡板:
// main.c
#include <iostream>
class Car{
public:
Car(){std::cout << "default constructor called" << std::endl;}
Car(Car &&rhs){std::cout << "move constructor called" << std::endl;}
~Car(){std::cout << "destructor called" << std::endl;}
};
int main() {
Car a = Car();
return 0;
}
使用来自终端的编译器参数运行它:
$ `g++ main.c -O0 -fno-elide-constructors'
产生所需的输出:
default constructor called
move constructor called
destructor called
destructor called
我也尝试将此参数用于 bazel,但我发现了这一点: bazel --copt flag。使用此命令构建后:
带 bazel:
$ bazel build --copt="-O0" --copt="-fno-elide-constructors" main:main
我收到警告:
cl : Command line warning D9002 : ignoring unknown option '-O0'
cl : Command line warning D9002 : ignoring unknown option '-fno-elide-constructors'
当我运行它时:
$ bazel-bin\main\main
我得到了输出:
default constructor called
destructor called
这意味着 bazel 没有通过禁用优化来构建项目。使用 bazel 构建项目时如何禁用所有优化? iI是否出现语法错误?这里可能有什么问题?
这是我在主文件夹中的 BUILD 文件:
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "main",
srcs = ["main.cc"],
)
我的窗口和编译器版本以防万一:
Microsoft Windows [Version 10.0.19042.1415]
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
【问题讨论】:
-
从错误信息
cl : Command line warning D9002可以看出编译器是cl。那是MSVC。此外,D9002之类的错误编号也是 MSVC 的风格,而不是 GCC。本杰明的回答显然是正确的,即哪个编译器正在获得这些选项。
标签: g++ compiler-optimization bazel