Compile your C++ programs with Google's Bazel
Including programs written in Java and other programming languages.
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:
And among others, for a more complete list see here .
You can also use npm regardless of OS:
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:
The example content for main.cpp
, a basic Hello, World!:
Contents of BUILD
:
And the contents of WORKSPACE
empty, to create just use the command:
Now to compile, run:
It will have an output similar to the one below:
To run use the run
parameter followed by the target indicated in BUILD
, in this case we use the binary name hello-world
:
Similar output:
Before printing, the tests and analyzes performed are described. If you want to run only the binary, run:
After build
it creates some directories inside your project, example based above:
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:
Enter the stage1
directory that has the most basic example and then analyze and test the others, example for stage1
:
Note the syntax stating that the
BUILD
file is inside the📁⠀main
directory
For more information visit the official page: https://bazel.build/ .
Comments