[An Introduction to GCC 学习笔记] 08 链接外部外部动态库C标准

Posted 漫小牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[An Introduction to GCC 学习笔记] 08 链接外部外部动态库C标准相关的知识,希望对你有一定的参考价值。

Shared vs Static Libraries

  • External libraries are usually provide in two forms: static libraries and shared libraries.
    • 1 Static libraries are the ‘.a’ files. When a program is linked against a static library, the machine code from the object files for any external functions used by the program is copied from the library into the final executable.
    • 2 Shared libraries are handled with a more advanced form of linking, which makes the executable file smaller. They use the extension ‘.so’, which stands for shared object.

Shared Libraries

  • An executable file linked against a shared library contains only a small table of the functions it requires, instead of the complete machine code from the object files for the external functions.
  • Before the executable file starts running, the machine code for the external functions is copied into memory from the shared libray on disk by the operating system ~ a process referred to as dynamic linking.
  • Dynamic linking makes executable files smaller and saves disk space, because one copy of a library can be shared between multiple programs.
  • Most operating systems also provide a virtual memory mechanism which allows one copy of a shared library in physical memory to be used by all running programs, saving memory as well as disk space.
  • Furthermore, shared libraries make it possible to update a library without recompiling the programs which use it, provided the interface to the library does not change.
  • Because of these advantages, gcc compilers programs to use share libraries by default on most systems, if they are available.
  • When the executable file is started, its loader function must find the shared library in order to load it into memory. The default search path is predefined set of system directories such as ‘/usr/local/lib’ and ‘/usr/lib’.
  • The enrivonmental variable LD_LIBRARY_PASH can be set as the search directories list.

C Language Standards

  • By default, gcc compilers programs using the GNU dialect of the C language, referred to as GNU C.
  • The dialect incorporates the official ANSI/ISO standard for the C language with several useful GNU extensions, such as nested functions and variable-size arrays.
  • Most ANSI/ISO programs will compile under GNU C without changes.
  • There are several options which control the dialect of C used by gcc. The most commonly-used options are ‘-ansi’ and ‘-pedantic’. The specific dialects of the C language for each standard can also be selected with the ‘-std’ option.

Selecting Specific Standard

  • The specific language standard used by GCC can be controlled with the ‘-std’ option. The following C language standards are supported:
    • ‘-std=c89’ or ‘-std=iso9899:1990’ ~The original ANSI/ISO C language standard(ANSI X3. 159-1989, ISO/IEC 9899:1990). GCC incorporate the corrections in the two ISO Technical Corrigenda to the original standard.
    • ‘-std=iso9899:199409’ ~ The ISO C language standard with ISO Amendment 1, published in 1994. This amendent was mainly concerned with internationalization, such as adding support for multibyte characters to the C library.
    • ‘-std=c99’ or ‘-std=iso9899:1999’ ~ This revised ISO C language standard, published in 1999 (ISO/IEC 9899:1999)

以上是关于[An Introduction to GCC 学习笔记] 08 链接外部外部动态库C标准的主要内容,如果未能解决你的问题,请参考以下文章

[An Introduction to GCC 学习笔记] 09 -Wall

[An Introduction to GCC 学习笔记] 14 优化问题3

[An Introduction to GCC 学习笔记] 14 优化问题3

[An Introduction to GCC 学习笔记] 13 优化问题2

[An Introduction to GCC 学习笔记] 10 Warn预编译

[An Introduction to GCC 学习笔记] 07 链接外部静态库