直接链接和作为静态库链接有什么区别

What's the difference between linking directly and linking as a static libarary

有区别吗
gcc a.c b.c -o b.out

gcc a.c -o a.o
ar rcs liba.a a.o

gcc b.c -la -o b.out

?

在什么情况下我应该二选一?

引用 dmckee's answer 的部分内容:

  • Dynamic linking can reduce total resource consumption (if more than one process shares the same library (including the version in "the same", of course)). I believe this is the argument that drives it its presence in most environments. Here "resources" includes disk space, RAM, and cache space. Of course, if your dynamic linker is insufficiently flexible there is a risk of DLL hell.

  • Dynamic linking means that bug fixes and upgrades to libraries propagate to improve your product without requiring you to ship anything.

  • Plugins always call for dynamic linking.
  • Static linking, means that you can know the code will run in very limited environments (early in the boot process, or in rescue mode).
  • Static linking can make binaries easier to distribute to diverse user environments (at the cost of sending a large and more resource hungry program).
  • Static linking may allow slightly faster startup times, but this depends to some degree on both the size and complexity of your program and on the details of the OSs loading strategy.

阅读完整答案以获取更多具体信息。

当您要多次重复使用已编译的模块时,创建库(变体 2)很好,而且项目确实很大(它可以节省一些编译时间)。否则没有区别。