简体   繁体   English

在 bazel 构建中使用宏

[英]Using macros with bazel build

I am using macro for enabling logging in my code.我正在使用宏来启用我的代码的日志记录。 Also, I am using bazel build.另外,我正在使用 bazel 构建。 Currently i need to change my .cpp file to include #define to enable this macro.目前我需要更改我的 .cpp 文件以包含#define以启用此宏。 Is there a way that i can provide this alongwith bazel build command ?有没有办法可以将它与bazel build命令一起提供?

One option would be to control the #define directly with the --cxxopt flag .一种选择是直接使用--cxxopt标志控制#define

Consider for example this code:例如,考虑以下代码:

#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;
}

Building without flags should result in the following:没有标志的建筑应该导致以下结果:

> bazel build :main
...
> ./bazel-bin/main
message: hello
else branch

While by settings the flags:而通过设置标志:

> bazel build --cxxopt=-D_MY_IDENTIFIER_ --cxxopt=-D_MY_MESSAGE_="\"hi\"" :main
> ./bazel-bin/main
message: hi
if branch

Same applies to bazel run :同样适用于bazel run

> bazel run --cxxopt=-D_MY_IDENTIFIER_ --cxxopt=-D_MY_MESSAGE_="\"hi\"" :main
...
message: hi
if branch

(tested only on linux) (仅在 linux 上测试)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM