gpt4 book ai didi

gcc - 为 gcc/g++ 指定库的不同方式

转载 作者:行者123 更新时间:2023-12-04 19:22:20 24 4
gpt4 key购买 nike

我很想知道通过以下两种方式(CC 可以是 g++ 或 gcc)将库(共享和静态)指定给 gcc/g++ 是否有任何实质性区别

CC -o output_executable /path/to/my/libstatic.a /path/to/my/libshared.so source1.cpp source2.cpp ... sourceN.cpp

对比
CC -o output_executable -L/path/to/my/libs -lstatic -lshared source1.cpp source2.cpp ... sourceN.cpp

我只能看到一个主要区别是直接传递完全指定的库名称可以更好地控制选择静态或动态版本,但我怀疑还有其他事情可能会对可执行文件的构建方式产生副作用或会在运行时表现,对吗?

安德烈亚。

最佳答案

好的,我可以根据一些实验和对 gcc 文档的深入阅读来回答自己:

来自 gcc 文档:http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

[...] The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -l option and specifying a file name is that -l surrounds library with lib' and.a' and searches several directories



这实际上也回答了有关在 gcc 命令行上直接指定目标文件的第三个选项的相关疑问(即在这种情况下,目标文件中的所有代码都将成为最终可执行文件的一部分,而使用存档时,只有目标文件真正需要的将被拉进来)。

关于gcc - 为 gcc/g++ 指定库的不同方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2537196/

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