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.
- 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