假设我有一个名为Vehicles.a
的静态库,其中有一个虚拟基类Vehicle
,还有许多从Vehicle
派生的类,例如Car
,{{ 1}},Truck
,Van
,所有这些类都具有虚拟成员函数,这些成员函数是基类的成员函数的替代。现在我有一个目标文件SUV
,其中有Client.o
,现在我正尝试通过链接到std::vector<*Vehicle>
从此Client.o
构建一个共享库,但是我知道对于最终共享库运到的某些特定客户,他们将永远不会使用类Vehicles.a
,在链接阶段如何从共享库中排除SUV
?由于我希望共享库仅包含必需的内容。
顺便说一句,共享库是使用g ++构建的。
答案 0 :(得分:1)
在链接阶段如何从共享库中排除SUV?
应该已经发生了。
如果您最终在共享库中找到SUV
,则Client.o
中的某物导致(可能是间接地)将其拉入。
使用g++ ... -Wl,-Map
检查链接映射或使用g++ ... -Wl,--cref
进行符号交叉引用应该可以回答问题。
或者,从SUV.o
中删除Vehicle.a
,重新链接共享库,然后检查现在缺少哪些符号:
# 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