Marko Bencik
Marko Bencik

Reputation: 396

Building libraries with bazel and hardcoding dependencies into them

Is it possible to hard code dependencies into the libraries build with bazel. The reason is that if I build somelib I can use it in the workspace but as soon as I copy the lib somewhere else I loose all dependencies (bazel cache). Witch creates a problem when I want to deploy the libraries into the system or install.

some_folder
|
thirdparty
|_WORKSPACE
|_somelib
|    |_src
|         |_ a.c
|         |_ BUILD
|    |_include
|         |_a.h
|_include
   |_ b.h

Upvotes: 0

Views: 1188

Answers (2)

iliaslgm
iliaslgm

Reputation: 1

just like Sebastian said (https://stackoverflow.com/a/55001169/14492972), you can use cc_binary + linkshared=True to build a library that will include all dependencies so that you won't have to link all external libraries separately. However, cc_binary doesn't have hdrs (headers) argument. You should add the header file that you want to be included for library usage in srcs.

Upvotes: 0

Sebastian Ärleryd
Sebastian Ärleryd

Reputation: 1824

It sounds like you want to build a fully statically linked library. This can be done in Bazel by building the library using cc_binary with the linkshared attribute set to True. According to the documentation you also have to name your library libfoo.so or similar.

What enables the static library here is cc_binary's linkstatic attributes behavior. When True, which is the default, all dependencies that can be linked statically into the binary will be. Note that linkstatic does NOT behave the same on cc_library, see the documentation.

So, basically you want something like this in your BUILD file

cc_binary(
    name = "libfoo.so",
    srcs = [...],
    hdrs = [...],
    linkshared = 1,
    #linkstatic = 1 # This is the default, you don't need to add this.
)

Good luck!

Upvotes: 2

Related Questions