gcc 是否有一些标志可以避免 link 未使用的共享库?

Is there some flags for gcc to avoid link with unused shared libraries?

echo "void main(){}" |gcc -xc -lm -lpthread -o test - && ldd test

这是一个例子。事实上,我不使用任何数学函数。但是 gcc 最终还是 link 和 libm.so。 在这种情况下,是否有任何优化标志告诉 gcc 不要 link 和 libm.so

echo "void main(){}" |gcc -xc -lm -lpthread -o test - && ldd test

-lm 明确要求 link 与 libm.so, gcc 作为好孩子就是按照你的要求

也许你有一个很好的理由 link with,gcc / ld 不知道你为什么这样做并且不能自己决定不按照你的要求,也没有选项-dontDoWhatIaskYouToDo ^^

如果您不想 link 使用该库,请不要要求 link 使用它

终于知道结果了。 -Wl,--as-needed 适用于这种情况。