# Bazel basics

Everything happens under your *drake/* folder.

```
cd drake
```

### Build

To build the whole folder:

```
bazel build //...
```

{% hint style="warning" %}
`bazel build` takes all available CPUs to compile. The compilation would fail if not enough memory is available. To [limit the number of concurrent tasks](https://docs.bazel.build/versions/master/user-manual.html#flag--jobs), use `bazel build //... --jobs=n` , where n is the number of CPUs used.
{% endhint %}

`//` means the root path of your drake folder, it equals `drake/`. `...` means build everything. Build everything can take a long time. We can speed up the building by narrowing down to a subfolder. For example, to build all targets under a subfolder:

```
bazel build //tools/...
```

Or to build a specific target:

```
bazel build //tools:drake_visualizer
```

`//tools` tells `bazel` that the path of the target is *drake/tools/*. What's followed after `:` is the target `drake_visualizer`. `bazel` will find the `BUILD.bazel` file under *drake/tools/* and build the target based on the rules defined in the `BUILD.bazel`.

### Run

To run a specific executable:

```
bazel run //examples/double_pendulum:double_pendulum_demo
```

`bazel run` will detect the relevant file modification. If the files are changed, this command will build first and then run immediately after the build. `:` is followed by the executable binary, in this case, `double_pendulum_demo`.

Another way to run the binary is to type the binary name directly in the terminal. It does not check the file change nor recompile. All the `bazel` binaries are put in the *drake/bazel-bin/* folder after being built. The detailed location of an executable is defined by `BUILD.bazel`.

```
bazel-bin/examples/double_pendulum/double_pendulum_demo
```

How to find where is the executable? Well, check the `BUILD.bazel` file and find the `drake_cc_binary` item. The executable sits in the `name` line.

### Debug

using gdb to debug when you find errors like segmentation fault comes handy. We could compile the executable with gdb by:

```
bazel build --compilation_mode=dbg //examples/multibody/inclined_plane_with_body
```

Then you could run the executable with gdb inspecting the variables and function calls. When there is a error and program crashes, we could trace back to where it went wrong.

```
gdb ./bazel-bin/examples/multibody/inclined_plane_with_body
(gdb) run # execute the program and record the runtime stack data
(gdb) bt # back trace to where the program crashes and figure the problem
(gdb) quit
```

### Test

This command applies when you wrote your own function and test cases. Then you could test your cases by:

```
bazel test //common:polynomial_test
```

### For more bazel commands

Commands in this page are enough to handle most cases.&#x20;

For advanced `bazel` usage, check out the [Drake document of bazel](https://drake.mit.edu/bazel.html#using-bazel).&#x20;

To learn more about `bazel`, please refer to [bazel official document](https://docs.bazel.build/versions/master/bazel-overview.html).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://drake.guzhaoyuan.com/introduction/bazel-basics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
