bazel的使用

bazel的使用

bazel是google开源的构建工具,可以支持多种语言的构建。这里来尝试一下如何在C++项目中使用bazel构建。

安装就不介绍了,在官网很详细,输入bazel --help

Usage: bazel <command> <options> ...

Available commands:
analyze-profile     Analyzes build profile data.
build               Builds the specified targets.
canonicalize-flags  Canonicalizes a list of bazel options.
clean               Removes output files and optionally stops the server.
coverage            Generates code coverage report for specified test targets.
dump                Dumps the internal state of the bazel server process.
fetch               Fetches external repositories that are prerequisites to the targets.
help                Prints help for commands, or the index.
info                Displays runtime info about the bazel server.
license             Prints the license of this software.
mobile-install      Installs targets to mobile devices.
query               Executes a dependency graph query.
run                 Runs the specified target.
shutdown            Stops the bazel server.
test                Builds and runs the specified test targets.
version             Prints version information for bazel.

看上去和maven差不太多,以一个实际项目来介绍一下基本命令的使用。

bazel build
在目录下建立test文件夹和WORKSPACE,并在test下创建两个文件,分别如下:

├── test
│   ├── BUILD
│   └── test.cc
└── WORKSPACE

内容如下:

BUILD:

package(default_visibility = ["//visibility:public"])

cc_binary(
    name = "test",
    srcs = [
        "test.cc",
    ],
)

WORKSPACE为空

test.cc:

#include <iostream>

int main() 
{
    printf("test \n");

    return 0;
}

其中:WORKSPACE和BUILD是bazel项目必须的文件,test.cc是我们自己定义的c++文件,.cc后缀是unix系统的后缀,.cpp是非unix系统。

构建项目
在test/目录下执行以下命令可以编译c++文件:

bazel build [target]

本例中:

bazel build test/...

注意是3个点!

编译成功的结果:

art@art:~/test$ bazel build test/...
INFO: Analyzed target //test:test (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //test:test up-to-date:
  bazel-bin/test/test
INFO: Elapsed time: 0.659s, Critical Path: 0.45s
INFO: 2 processes: 2 linux-sandbox.
INFO: Build completed successfully, 3 total actions


art@art:~/test$ ./bazel-bin/test/test
test 


目录结果变为:

art@art:~/test$ tree
.
├── bazel-bin -> /home/art/.cache/bazel/_bazel_art/2c6e9f0f22570755ccf79d29dc14971b/execroot/__main__/bazel-out/k8-fastbuild/bin
├── bazel-out -> /home/art/.cache/bazel/_bazel_art/2c6e9f0f22570755ccf79d29dc14971b/execroot/__main__/bazel-out
├── bazel-test -> /home/art/.cache/bazel/_bazel_art/2c6e9f0f22570755ccf79d29dc14971b/execroot/__main__
├── bazel-testlogs -> /home/art/.cache/bazel/_bazel_art/2c6e9f0f22570755ccf79d29dc14971b/execroot/__main__/bazel-out/k8-fastbuild/testlogs
├── test
│   ├── BUILD
│   └── test.cc
└── WORKSPACE

5 directories, 3 files

bazel-bin/test/test.runfiles/main/test目录下有二进制文件,运行:

art@art:~/test/bazel-bin/test/test.runfiles/__main__/test$ ls
test

$ ./test
test

至此bazel编译成功!

qita jieshao :

bazel run

bazel build用来编译cpp为二进制文件,除此之外还可以直接运行cpp文件中的main函数。

bazel query

用来查看依赖树。

bazel test

类似mvn test,运行代码中的单测。

bazel clean

清除编译的结果,类似mvn clean。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值