Calmarius
Calmarius

Reputation: 19431

How can I control what symbols are exported from a shared library in Bazel?

I'm learning Bazel, because I have to use it at work. I have a simple build rule that creates a library from a single file and I would like control what is exported by the linker by using a linker version file. (I'm on Linux)

So I tried:

cc_library (
    name ="thelib",
    srcs = ["lib.cpp"],
    linkopts = ["-Wl,--version-script,lib.ver"]
)

And it tells me "No such file or directory".

What have I tried:

At this point I completely ran out of ideas. Any help is appreciated.

Upvotes: 1

Views: 1974

Answers (1)

Benjamin Peterson
Benjamin Peterson

Reputation: 20520

First of all, shared libraries are generally declared with cc_binary in conjunction with its linkshared attribute rather than cc_library. This is counterintuitive but reflects the intention that cc_binary creates a transitive link while cc_library declares an intermediate library. (More adventurous folks may try out cc_shared_library

Second of all, it's possible to use version scripts (and other linker scripts) by using them in linkopts and declaring them as a dependency in deps.

So, all together:

cc_binary(
   name = 'mylib.so',
   linkshared = True,
   srcs = ['mylib.cc'],
   linkopts = ['-Wl,-version-script=$(location version-script.lds)'],
   deps = [
      'version-script.lds',
   ]
)

Upvotes: 2

Related Questions