Compile your C++ programs with Google's Bazel

Including programs written in Java and other programming languages.


Compile your C++ programs with Google's Bazel


Bazel is an open source tool for building automation and testing software developed by Google .

That is, Bazel is an alternative to Make(from the GNU project) or Ant and Maven, both to compile programs in Java, developed by the company: Apache .

Bazel was written with the Starlark programming language, a dialect of Python .

Bazel builds almost all Google products: Google Search, GMail, Google Docs, …

What is the purpose of using Bazel ?

Bazel is designed to work in a standardized way across Google products.

Usually companies tend to create their own solutions for reasons of standardization and it also serves to avoid “free advertising of products from other companies”, even more Google that one of its main sources of revenue is precisely: advertising.

But, you can migrate your projects if you want!


Installation

There are several ways you can install it. You can use your operating system’s package manager, examples below:

emerge dev-util/bazel # Gentoo
sudo pacman -S bazel # Arch via AUR
sudo dnf install bazel4 # Fedora após instalar dnf-plugins-core
pkg install devel/bazel # FreeBSD via Ports

And among others, for a more complete list see here .

You can also use npm regardless of OS:

sudo npm install -g @bazel/bazelisk


Usage

The most basic example of all, in this case for C++, would be a project with 3 files:

  • Your program: main.cpp
  • The BUILD file that would have the macros to be processed
  • And an empty file named WORKSPACE, it serves to indicate the root of the project.

In short:

MYPROJECT/
├── BUILD
├── WORKSPACE
└── main.cpp

0 directories, 3 files

The example content for main.cpp, a basic Hello, World!:

#include <iostream>

int main(){
  std::cout << "Hello, World!" << '\n';
}

Contents of BUILD:

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

And the contents of WORKSPACE empty, to create just use the command:

> WORKSPACE

Now to compile, run:

bazel build hello-world

It will have an output similar to the one below:

INFO: Analyzed target //:hello-world (37 packages loaded, 163 targets configured).
INFO: Found 1 target...
Target //:hello-world up-to-date:
  bazel-bin/hello-world
INFO: Elapsed time: 6.229s, Critical Path: 0.55s
INFO: 6 processes: 4 internal, 2 linux-sandbox.
INFO: Build completed successfully, 6 total actions

To run use the run parameter followed by the target indicated in BUILD, in this case we use the binary name hello-world:

bazel run hello-world

Similar output:

INFO: Analyzed target //:hello-world (36 packages loaded, 163 targets configured).
INFO: Found 1 target...
Target //:hello-world up-to-date:
  bazel-bin/hello-world
INFO: Elapsed time: 3.666s, Critical Path: 0.66s
INFO: 6 processes: 4 internal, 2 linux-sandbox.
INFO: Build completed successfully, 6 total actions
INFO: Build completed successfully, 6 total actions
Hello, World!

Before printing, the tests and analyzes performed are described. If you want to run only the binary, run:

./bazel-bin/hello-world

After build it creates some directories inside your project, example based above:

$ ls
📁⠀bazel-MYPROJECT/  📁⠀bazel-bin/  📁⠀bazel-out/  📁⠀bazel-testlogs/  BUILD  main.cpp  WORKSPACE

Use the tree command (if you have it installed) for more details.


More complete examples

The example we show is an “informal” way to create a project to be compiled with Bazel . The correct way can be found in Bazel repository, example:

git clone https://github.com/bazelbuild/examples
cd bazelbuild
tree cpp-tutorial

examples
└── cpp-tutorial
    ├──stage1
    │  ├── main
    │  │   ├── BUILD
    │  │   └── hello-world.cc
    │  └── WORKSPACE
    ├──stage2
    │  ├── main
    │  │   ├── BUILD
    │  │   ├── hello-world.cc
    │  │   ├── hello-greet.cc
    │  │   └── hello-greet.h
    │  └── WORKSPACE
    └──stage3
       ├── main
       │   ├── BUILD
       │   ├── hello-world.cc
       │   ├── hello-greet.cc
       │   └── hello-greet.h
       ├── lib
       │   ├── BUILD
       │   ├── hello-time.cc
       │   └── hello-time.h
       └── WORKSPACE

Enter the stage1 directory that has the most basic example and then analyze and test the others, example for stage1:

cd stage1
bazel build //main:hello-world

Note the syntax stating that the BUILD file is inside the 📁⠀main directory

For more information visit the official page: https://bazel.build/ .


cpp cppdaily java


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles