> ## Documentation Index
> Fetch the complete documentation index at: https://aspect.build/llms.txt
> Use this file to discover all available pages before exploring further.

# Build external dependencies from source

> Build C/C++ external dependencies like libmagic from source with Bazel via the Bazel Central Registry for hermetic, reproducible cross-platform builds.

The non-hermetic build from the last lesson has a lot of downsides. Let’s have Bazel manage the dependency instead.

Many libraries already have `BUILD` files in the Bazel Central Registry (BCR). We can search for the `libmagic` library we used in the last lesson, and find that it’s there:

[https://registry.bazel.build/modules/libmagic](https://registry.bazel.build/modules/libmagic)

This means we can avoid depending on the state of the local machine, and know that all developers will use the same version of this library. It’s also friendly for cross-compilation, since Bazel will build libmagic for the target platform, not the host.

To install, add to `MODULE.bazel` :

```python theme={null}
bazel_dep(name = "libmagic", version = "5.46")
```

And comment out the `cxx_builtin_include_directories` attribute we added in the previous lesson, which would conflict.

This module ought to have a cc\_library that we can add into our deps. Let's see what’s in it:

```cpp theme={null}
% bazel query --output=label_kind @libmagic//...
cc_binary rule @libmagic//:file
genrule rule @libmagic//:gen_magic
expand_template rule @libmagic//:gen_magic_h
cc_library rule @libmagic//:lib
alias rule @libmagic//:libmagic
cc_shared_library rule @libmagic//:magic
```

We want the `cc_library` so that we statically-link the libmagic library into our application, so change `BUILD` to have `deps = ["@libmagic//:lib"]`

Now when we run the application, we compile `libmagic` sources as part of our build. Note that we want to be hermetic, and the `file` command reads some “magic” data at runtime, so we supply that as well.

```python theme={null}
cc_binary(
	  name = "magic",
	  srcs = ["magic.c"],
    deps = ["@libmagic//:lib"],
    env = {
        "MAGIC": "$(rootpath @libmagic//:magic.mgc)"
    },
    data = [
        "@libmagic//:magic.mgc"
    ],
)
```

Then run `bazel run magic $PWD/BUILD`
to get:
`MIME type: text/plain`

## What if the module isn’t on BCR?

It’s common practice to write your own BUILD files for third-party libraries. The conventional location is `third_party/BUILD.libmagic` for example.

Please consider upstreaming your BUILD files to the Bazel Central Registry! This helps you, as other maintainers can improve the code. It also helps your fellow engineers who can skip this step next time.
