[英]Using macros with bazel build
我正在使用宏来启用我的代码的日志记录。 另外,我正在使用 bazel 构建。 目前我需要更改我的 .cpp 文件以包含#define
以启用此宏。 有没有办法可以将它与bazel build
命令一起提供?
一种选择是直接使用--cxxopt
标志控制#define
。
例如,考虑以下代码:
#include <iostream>
#ifndef _MY_MESSAGE_
#define _MY_MESSAGE_ "hello"
#endif
int main(int argc, char const *argv[]) {
std::cerr << "message: " _MY_MESSAGE_ "\n";
#ifdef _MY_IDENTIFIER_
std::cerr << "if branch \n";
#else
std::cerr << "else branch \n";
#endif
return 0;
}
没有标志的建筑应该导致以下结果:
> bazel build :main
...
> ./bazel-bin/main
message: hello
else branch
而通过设置标志:
> bazel build --cxxopt=-D_MY_IDENTIFIER_ --cxxopt=-D_MY_MESSAGE_="\"hi\"" :main
> ./bazel-bin/main
message: hi
if branch
同样适用于bazel run
:
> bazel run --cxxopt=-D_MY_IDENTIFIER_ --cxxopt=-D_MY_MESSAGE_="\"hi\"" :main
...
message: hi
if branch
(仅在 linux 上测试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.