bazel C++ 简单demo

本文介绍了如何在Ubuntu上安装Bazel构建工具,并通过示例展示了如何使用Bazel构建C++项目。首先提供了详细的安装步骤,包括安装所需软件包、下载及运行Bazel安装程序。接着,通过具体的C++项目案例,解释了Bazel构建文件的编写方法及其依赖关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

这里写图片描述

<think>首先,用户的问题是:“我想学习Bazel的使用方法 请问Bazel 入门教程 使用指南”。这直接要求提供Bazel的入门教程和使用指南。 根据系统级指令: - 所有行内数学表达式必须使用$...$格式,但这里可能不涉及数学表达式,因为Bazel是构建工具。 - 独立公式必须使用$$...$$格式并单独成段,同样可能不适用。 - LaTeX语法正确:确保在需要时正确使用,但这里可能不需要。 - 使用中文回答:所有回答必须用中文。 - 生成相关问题:在回答末尾,生成相关问题。 - 回答中引用的段落末尾自然地添加引用标识,例如[^1]。 用户可见层: - 回答结构清晰,帮助用户逐步解决问题。 - 尽量保证回答真实可靠。 - 参考站内引用:提供了三个引用: - [^1]: 关于bazel命令的帮助信息。 - [^2]: Bazel的简介和批评。 - [^3]: Bazel在Ubuntu上的安装教程。 我需要基于这些引用构建回答,因为指令说“上述引用仅供参考,并不是用户真正的需求,请尽可能多的参考用户上一次的问题和你的回答”。但用户的问题是新问题,没有提到上一次问题。所以,我应该基于当前问题和提供的引用来回答。 结构化回答: 1. **介绍Bazel**:简要解释Bazel是什么,基于引用[^2]。 2. **安装Bazel**:基于引用[^3]提供安装步骤。 3. **基本使用**:基于引用[^1]解释常用命令。 4. **入门指南**:提供简单的教程,如创建一个简单的项目。 5. **生成相关问题**:在末尾添加相关问题。 确保回答真实可靠:Bazel是一个真实的工具,我应该基于一般知识和引用提供准确信息。 用中文回答。 步骤: - **开头**:直接回应用户的问题。 - **主体**: - 什么是Bazel? - 如何安装? - 基本命令和使用方法。 - 简单示例。 - **结尾**:生成相关问题。 添加引用标识:在引用信息时,在末尾添加[^1]、[^2]等。 例如,在描述命令时引用[^1],在介绍时引用[^2],在安装步骤引用[^3]。 生成相关问题:类似示例,列出2-3个相关问题。 最终回答结构: - 清晰段落。 - 可能包括代码块,如果适用。 - 末尾有“相关问题”部分。</think>### Bazel入门教程和使用指南 Bazel是一个开源的构建和测试工具,由Google开发,用于管理大型代码库(如monorepo模式)。它支持多种编程语言(如Java、C++、Python等),并能跨平台构建输出。Bazel的核心优势在于高效处理依赖关系和增量构建,特别适合团队协作的大型项目[^2]。不过,需要注意的是,Bazel在IDE支持和某些场景下可能不如其他工具(如Maven或Gradle)便捷[^2]。下面我将一步步指导你入门Bazel,从安装到基本使用,确保内容清晰实用。 #### 1. **安装Bazel** 在开始使用Bazel前,需要先安装它。Bazel支持多种操作系统,这里以Ubuntu为例(其他系统可参考[官方文档](https://bazel.build))。以下是安装步骤,基于引用[^3]: - 添加Bazel的APT仓库并安装: ```bash sudo apt install curl curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add - echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list sudo apt-get update sudo apt-get install bazel # 安装最新版本 ``` - 验证安装:运行`bazel --version`,如果输出类似`bazel 6.x.x`,则表示安装成功。 - 升级Bazel:使用`sudo apt-get install --only-upgrade bazel`命令[^3]。 提示:Bazel依赖Java环境(建议JDK 8或更高),确保系统中已安装Java。 #### 2. **Bazel基本概念** 理解Bazel的工作原理是关键: - **WORKSPACE文件**:定义项目根目录,用于管理外部依赖。 - **BUILD文件**:在每个源代码目录中定义构建规则,指定如何编译目标(如库、二进制文件或测试)。 - **目标(Targets)**:Bazel的构建单元,例如一个可执行文件或测试套件。 - 优势:Bazel使用缓存和并行构建加速编译,但需注意在小型项目中可能显得繁琐[^2]。 #### 3. **基本命令和使用方法** Bazel通过命令行操作,常用命令基于引用[^1]的总结。以下是核心命令和示例: - **构建项目**:使用`bazel build`命令编译指定目标。例如,创建一个简单的“Hello World”项目: 1. 创建项目目录: ```bash mkdir my_project && cd my_project touch WORKSPACE # 空文件,表示项目根目录 mkdir src && touch src/main.cc # 示例C++文件 ``` 2. 编辑`src/main.cc`: ```cpp #include <iostream> int main() { std::cout << "Hello, Bazel!" << std::endl; return 0; } ``` 3. 创建`BUILD`文件(在`src/`目录下): ```python cc_binary( name = "hello", srcs = ["main.cc"], ) ``` 4. 运行构建: ```bash bazel build //src:hello # 编译目标 ``` 输出文件在`bazel-bin/src/hello`,运行它:`./bazel-bin/src/hello`,将显示“Hello, Bazel!”。 - **其他常用命令**: - `bazel run //target`:构建并运行目标(如`bazel run //src:hello`)。 - `bazel test //target`:运行测试(需在BUILD文件中定义测试规则)。 - `bazel clean`:清理构建输出。 - `bazel query`:分析依赖关系(如`bazel query 'deps(//src:hello)'`)。 - `bazel help`:查看完整命令帮助[^1]。 #### 4. **入门教程:一个简单Java项目示例** 为了加深理解,这里提供一个Java项目的快速指南: 1. 创建项目结构: ```bash mkdir java_app && cd java_app touch WORKSPACE mkdir -p src/main/java/com/example touch src/main/java/com/example/Main.java ``` 2. 编辑`Main.java`: ```java package com.example; public class Main { public static void main(String[] args) { System.out.println("Bazel Java Demo"); } } ``` 3. 创建`BUILD`文件(在`src/main/java/com/example/`目录): ```python java_binary( name = "app", srcs = ["Main.java"], main_class = "com.example.Main", ) ``` 4. 构建和运行: ```bash bazel build //src/main/java/com/example:app bazel run //src/main/java/com/example:app # 输出"Bazel Java Demo" ``` #### 5. **最佳实践和注意事项** - **学习资源**:官方文档([bazel.build](https://bazel.build))是最全面的教程,包含示例和指南。 - **优点**:Bazel适合monorepo项目,能高效处理跨语言依赖[^2]。 - **缺点**:IDE支持较弱(如VS Code或IntelliJ需插件),且配置复杂,小型项目建议用更简单工具(如Maven)[^2]。 - **调试**:使用`bazel analyze-profile`分析构建性能,优化BUILD文件。 通过以上步骤,你应该能开始使用Bazel进行基本构建。记住,实践是关键——尝试修改示例项目来熟悉规则定义。Bazel的学习曲线较陡,但掌握后能显著提升大型项目的构建效率[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值