2

I have built a lib.so using make rule in bazel. How do I link this external lib.so to a regular cc_library rule. I tried adding it in deps, but the guide suggests that deps can have cc_library or objc_library targets.

Also, do I need to pass any specific linking options, and how can I read more about them?

Saxtheowl
  • 4,136
  • 5
  • 23
  • 32
Aditya
  • 5,509
  • 4
  • 31
  • 51

1 Answers1

2

In the BUILD file, create a cc_library target that imports the built lib.so for other cc_library targets to depend on:

cc_library(
    name = "lib",
    srcs = ["lib.so"],
    linkopts = ["...", "..."],
)

See the documentation on C++ use cases for more information.

Jin
  • 12,748
  • 3
  • 36
  • 41
  • "..." denotes a placeholder for possible linking options? Is there any documentation explaining them? – Aditya Oct 10 '19 at 20:48
  • Also, this gives `missing input file '@name//:lib.so'` – Aditya Oct 10 '19 at 21:51
  • Yes, https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.linkopts - the options are the exact flags you would pass to the linker, like `-ldl` or `-lm`. – Jin Oct 11 '19 at 18:35
  • Where is `lib.so` in `@name`? Is it in the top level package? Or is it created in a nested directory? If it's the latter, try `@name//:path/to/dir/lib.so`. – Jin Oct 11 '19 at 18:36
  • lib.so is built as part of make rule. It is not in the top level directory, but even with the suggestion i get the error `ERROR: missing input file '@name//:code/lib.so'`. I also tried adding a deps rule, but that did not help either. – Aditya Oct 14 '19 at 17:53
  • The `missing input file` error means that the file label is incorrect. You should be able to get the actual file label of the built `lib.so` using `bazel query //path/to/package:*` (`:*` shows all file labels in a package). – Jin Oct 14 '19 at 20:52
  • 1
    The bazel offical document on using external library is basicaly a joke: https://docs.bazel.build/versions/main/cpp-use-cases.html#adding-dependencies-on-precompiled-libraries, it is not clear on how the library is linked, and the header file is included, are there any samples on actually using an external library, from a specific path? – Baiyan Huang Aug 29 '21 at 09:58