bazel alwayslink = true 是什么意思?
What does bazel alwayslink = true mean?
我是 bazel 的新手。这是 bazel 文档中的解释:
https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.alwayslink
alwayslink
Boolean; optional; nonconfigurable; default is 0
If 1, any binary that depends (directly or indirectly) on this C++
library will link in all the object files for the files listed in
srcs, even if some contain no symbols referenced by the binary. This
is useful if your code isn't explicitly called by code in the binary,
e.g., if your code registers to receive some callback provided by some
service.
最后一句不太明白:e.g., if your code registers to receive some callback provided by some service.
谁能举个例子?谢谢!
e.g., if your code registers to receive some callback provided by some service.
AIUI,cc_binary
构建共享库/DLL时就是这种情况。您需要链接器保留所有符号,即使未使用也是如此,因为在运行时加载 .so/.dll 的另一个二进制文件可能需要这些符号。
如果目标文件未被程序的其余部分引用,大多数 link 用户将删除它们,即使它们包含动态初始化。
例如:
struct S { S(); }
S s;
S::S() {
std::cout << "Hello, World!";
}
此代码在动态初始化期间打印 Hello, World!
。全局变量s
的构造函数打印出来,在动态初始化时调用。
如果您 link 将此代码与程序一起使用,它可能会被删除,因此 Hello, World!
将不会被程序打印。
alwayslink
标志阻止了这种情况,并强制程序包含动态初始化。
通常的用例是当动态初始化导致东西注册时。例如它可能是某个应用程序的“插件”,它的动态初始化将它添加到插件的全局列表中。
它与共享库和静态库无关。
我是 bazel 的新手。这是 bazel 文档中的解释:
https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.alwayslink
alwayslink
Boolean; optional; nonconfigurable; default is 0
If 1, any binary that depends (directly or indirectly) on this C++ library will link in all the object files for the files listed in srcs, even if some contain no symbols referenced by the binary. This is useful if your code isn't explicitly called by code in the binary, e.g., if your code registers to receive some callback provided by some service.
最后一句不太明白:e.g., if your code registers to receive some callback provided by some service.
谁能举个例子?谢谢!
e.g., if your code registers to receive some callback provided by some service.
AIUI,cc_binary
构建共享库/DLL时就是这种情况。您需要链接器保留所有符号,即使未使用也是如此,因为在运行时加载 .so/.dll 的另一个二进制文件可能需要这些符号。
如果目标文件未被程序的其余部分引用,大多数 link 用户将删除它们,即使它们包含动态初始化。
例如:
struct S { S(); }
S s;
S::S() {
std::cout << "Hello, World!";
}
此代码在动态初始化期间打印 Hello, World!
。全局变量s
的构造函数打印出来,在动态初始化时调用。
如果您 link 将此代码与程序一起使用,它可能会被删除,因此 Hello, World!
将不会被程序打印。
alwayslink
标志阻止了这种情况,并强制程序包含动态初始化。
通常的用例是当动态初始化导致东西注册时。例如它可能是某个应用程序的“插件”,它的动态初始化将它添加到插件的全局列表中。
它与共享库和静态库无关。