[英]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:我尝试了什么:
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. bazel info --show_make_env
列出了 make 变量,它向我显示了一个名为workspace
的变量,所以我然后尝试$(workspace)/lib/lib.ver
但后来它说$(workspace) not defined
,所以 Bazel 是个骗子。-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" 界面我不希望上面出现任何其他垃圾)。 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.