简体   繁体   English

如何控制从 Bazel 中的共享库导出的符号?

[英]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.我正在学习 Bazel,因为我必须在工作中使用它。 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.我有一个简单的构建规则,它从单个文件创建一个库,我想通过使用 linker 版本文件来控制 linker 导出的内容。 (I'm on Linux) (我在 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:我尝试了什么:

  • I tried a path relative to the directory I issue the bazel build command with no avail.我尝试了一个相对于我发出 bazel build 命令的目录的路径,但无济于事。
  • cc_library()'s documentation says "linkopts" support make variable substitution so I listed the make variables with bazel info --show_make_env it showed me a variable called workspace so I then tried $(workspace)/lib/lib.ver but then it says $(workspace) not defined so Bazel is a liar. cc_library() 的文档说“linkopts”支持 make 变量替换,所以我用bazel info --show_make_env列出了 make 变量,它向我显示了一个名为workspace的变量,所以我然后尝试$(workspace)/lib/lib.ver但后来它说$(workspace) not defined ,所以 Bazel 是个骗子。
  • The only thing that works is spelling the absolute path to the linker script but I don't want to push that.唯一有效的是拼写 linker 脚本的绝对路径,但我不想推动它。
  • cc_library() has a win_def_file option but guess what, that's Windows only. cc_library() 有一个 win_def_file 选项,但你猜怎么着,那只是 Windows。
  • A self-closed github issue suggests that I should pass the filename as a separate argument, it doesn't work either.自封闭 github 问题表明我应该将文件名作为单独的参数传递,它也不起作用。
  • Using -fvisiblity=hidden and export using __attribute__ to export is not an option because the standard C++ library overrides it and forces exporting of the symbols you don't want to appear on the interface (the library is used using extern "C" interface only I don't want any other garbage appear on it).使用-fvisiblity=hidden并使用__attribute__导出不是一个选项,因为标准 C++ 库会覆盖它并强制导出您不想出现在界面上的符号(该库仅使用 extern "C" 界面我不希望上面出现任何其他垃圾)。
  • There doesn't seem to be an option in Bazel to specify the symbols to export natively. Bazel 中似乎没有选项来指定要本地导出的符号。

At this point I completely ran out of ideas.在这一点上,我完全没有想法了。 Any help is appreciated.任何帮助表示赞赏。

First of all, shared libraries are generally declared with cc_binary in conjunction with its linkshared attribute rather than cc_library .首先,共享库通常使用cc_binary及其linkshared属性而不是cc_library This is counterintuitive but reflects the intention that cc_binary creates a transitive link while cc_library declares an intermediate library.这是违反直觉的,但反映了cc_binary创建传递链接而cc_library声明中间库的意图。 (More adventurous folks may try out cc_shared_library (喜欢冒险的人可以试试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 .其次,可以通过在linkopts中使用版本脚本(和其他 linker 脚本)并将它们声明为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',
   ]
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM