2.4.5. Linking static libraries with the GCC


To link static libraries, bundle them as archives that contain object files. After linking, they become part of the resulting executable file. Static linking overrides the default dynamic linking behavior.

注意

Red Hat discourages use of static linking for security reasons. See Static and dynamic linking. Use static linking only when necessary, especially against libraries provided by Red Hat.

Most libraries that are part of Red Hat Enterprise Linux support dynamic linking only. The remaining steps in this procedure apply only to libraries that do not support dynamic linking.

Prerequisites

See Static and dynamic linking

Procedure

  • To link a program from source and object files, adding a statically linked library foo, which is to be found as a file libfoo.a.

    1. Change to the directory containing your code.
    2. Compile the program source files with headers of the foo library:

      $ gcc ... -Iheader_path -c ...

      Replace header_path with a path to a directory containing the header files for the foo library.

    3. Link the program with the foo library:

      $ gcc ... -Llibrary_path -lfoo ...

      Replace library_path with a path to a directory containing the file libfoo.a.

    4. To run the program later:

      $ ./program
      警告

      The -static GCC option related to static linking forbids all dynamic linking. Instead, use the -Wl,-Bstatic and -Wl,-Bdynamic options to control linker behavior more precisely. See Static and dynamic libraries with GCC.

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部