【问题标题】:What happens with unused functions in C programs?C 程序中未使用的函数会怎样?
【发布时间】:2022-12-02 05:52:44
【问题描述】:

Let us suppose that I write a program that includes the stdio.h header and this program uses only the printf function. When I compile this program, does the executable file include all the function described in stdio.h? Or it will include only the necessary code related to the printf function?

  • It depends a lot on the linker. Generally, they can pull only the code they need from the object libraries (assuming the Standard C libraries are sufficiently \'modular\' to allow that).
  • Nowadays, it\'s also pretty common to link with shared libraries, so your executable will then have a few \"hooks\" into the library that is shared with many other executables.
  • If you link statically, the typical linker by default includes only the object modules that provide functions and variables you use, plus object modules that provide functions they use, and so on. If somebody has structured the library so that functions are in separate object modules, then not many extra functions will be included in your executable. If they built the library as one big object module, then everything will be included (unless removed by dead-code elimination).
  • If you link dynamically, the library object modules will not be included in your executable. They will be loaded at run-time when the program needs them.

标签: c compilation


【解决方案1】:
猜你喜欢
  • 2017-09-29
  • 1970-01-01
  • 2010-10-26
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
  • 2021-04-03
  • 2021-12-12
  • 2023-03-14
相关资源
最近更新 更多