bazel C++ 简单demo

bazel 安装
https://docs.bazel.build/versions/master/install-ubuntu.html
推荐方式:
Installing using binary installer
The binary installers are on Bazel’s GitHub releases page.
The installer contains the Bazel binary and the required JDK. Some additional libraries must also be installed for Bazel to work.

1.Install required packages

sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python

2.Download Bazel

Note: In the installer file names listed in this document, replace with the appropriate Bazel version number.

Go to Bazel’s GitHub releases page.

Download the binary installer bazel--installer-linux-x86_64.sh. This installer contains the Bazel binary and the required JDK, and can be used even if JDK is already installed.

Note that bazel--without-jdk-installer-linux-x86_64.sh also exists. It is a version without embedded JDK 8. Only use this installer if you already have JDK 8 installed.

3.Run the installer

Run the installer:
使用bazel-0.9.0-installer-linux-x86_64.sh
download from here: 链接: https://pan.baidu.com/s/1jJbpoR8 密码: 6dg8

chmod +x bazel-<version>-installer-linux-x86_64.sh
./bazel-<version>-installer-linux-x86_64.sh --user

The --user flag installs Bazel to the @HOME/bin directory on your system and sets the .bazelrc path to $HOME/.bazelrc. Use the --help command to see additional installation options.

  1. Set up your environment(optional)

If you ran the Bazel installer with the --user flag as above, the Bazel executable is installed in your $HOME/bin directory. It’s a good idea to add this directory to your default paths, as follows:

export PATH="$PATH:$HOME/bin"

You can also add this command to your ~/.bashrc file.

原文地址:
https://docs.bazel.build/versions/master/tutorial/cpp.html
代码路径:
git clone https://github.com/bazelbuild/examples/
目录结构:
这里写图片描述
打开cpp-tutorial/stage1/main/BUILD:

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
)

回到WORKPLACE文件所在路径: ./cpp-tutorial/stage1/
Build the project

bazel build //main:hello-world

输出:

INFO: Found 1 target...
Target //main:hello-world up-to-date:
  bazel-bin/main/hello-world
INFO: Elapsed time: 2.267s, Critical Path: 0.25s

Now test your freshly built binary:

bazel-bin/main/hello-world

可视化项目依赖:
在workspace root:

bazel query --nohost_deps --noimplicit_deps 'deps(//main:hello-world)' \
  --output graph
digraph mygraph {
  node [shape=box];
"//main:hello-world"
"//main:hello-world" -> "//main:hello-world.cc"
"//main:hello-world" -> "@opencv//:opencv"
"@opencv//:opencv"
"@opencv//:opencv" -> "@opencv//:include/opencv2/gpu/device/border_interpolate.hpp\n@opencv//:lib/libopencv_calib3d.so.3.1\n@opencv//:include/eigen3/Eigen/src/SparseCore/SparseDiagonalProduct.h\n@opencv//:include/opencv2/core/cuda/simd_functions.hpp\n@opencv//:include/opencv2/reg/mappergradsimilar.hpp\n@opencv//:lib/libprotobuf.so.9.0.1\n@opencv//:include/opencv2/stitching/detail/util.hpp\n@opencv//:include/tf/bazel-genfiles/tensorflow/cc/ops/user_ops_internal.h\n...and 1262 more items"
"@opencv//:include/opencv2/gpu/device/border_interpolate.hpp\n@opencv//:lib/libopencv_calib3d.so.3.1\n@opencv//:include/eigen3/Eigen/src/SparseCore/SparseDiagonalProduct.h\n@opencv//:include/opencv2/core/cuda/simd_functions.hpp\n@opencv//:include/opencv2/reg/mappergradsimilar.hpp\n@opencv//:lib/libprotobuf.so.9.0.1\n@opencv//:include/opencv2/stitching/detail/util.hpp\n@opencv//:include/tf/bazel-genfiles/tensorflow/cc/ops/user_ops_internal.h\n...and 1262 more items"
"//main:hello-world.cc"
}

将终端输出文字复制到GraphViz:
将文字复制到文档中,保存,右击->open with XDot
这里写图片描述

cpp-tutorial/stage2/main/BUILD:

cc_library(
    name = "hello-greet",
    srcs = ["hello-greet.cc"],
    hdrs = ["hello-greet.h"],
)

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
    deps = [
        ":hello-greet",
    ],
)

Bazel first builds the hello-greet library (using Bazel’s built-in cc_library rule, then the hello-world binary. The deps attribute in the hello-world target tells Bazel that the hello-greet library is required to build the hello-world binary.

转到cpp-tutorial/stage2 directory

bazel build //main:hello-world
bazel-bin/main/hello-world

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值