gpt4 book ai didi

c++ - 做共享库的时候能不能显式排除一些类?

转载 作者:行者123 更新时间:2023-11-28 04:35:54 25 4
gpt4 key购买 nike

假设我有一个名为 Vehicles.a 的静态库, 其中它有一个虚拟基类 Vehicle ,以及许多派生自 Vehicle 的类, 比如 Car , Truck , Van , SUV ,所有这些类都有虚成员函数,它们覆盖了基类的成员函数。现在我有一个目标文件 Client.o , 其中有 std::vector<*Vehicle> ,现在我正在尝试从这个 Client.o 构建一个共享库通过链接到 Vehicles.a , 但我知道对于最终共享库交付给的某些特定客户,他们永远不会使用类 SUV , 我怎样才能排除 SUV在链接阶段从共享库?因为我希望共享库只包含必要的内容。

顺便说一句,共享库是使用 g++ 构建的。

最佳答案

how can i exclude SUV from the shared library during linking stage?

这应该已经发生了。

如果您最终在共享库中使用 SUVClient.o 中的某些东西 会导致它被拉入(可能是间接)。

使用 g++ ... -Wl,-Map 检查链接映射或使用 g++ ... -Wl,--cref 的符号交叉引用应该回答什么东西是。

或者,从 Vehicle.a 中删除 SUV.o,重新链接您的共享库,并检查现在缺少的符号:

# First make sure the library is linked with all required symbols:
g++ -shared -o ... Client.o Vehicle.a -Wl,--no-undefined

# Now remove SUV.o from Vehicle.a
ar d Vehicle.a SUV.o

# This should now fail, and tell you which object requires SUV.o
g++ -shared -o ... Client.o Vehicle.a -Wl,--no-undefined

关于c++ - 做共享库的时候能不能显式排除一些类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51452210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com