How to generate static libraries and dynamic libraries

Source: Internet
Author: User


For a compiled language, compilers always have to go through preprocessing, compiling, linking and so on. As an example of C + +: The preprocessor (CPP) merges #include header files from each. c/.cpp file into the. c/.cpp file, and macros them, generating several compilation units. In general, each. c/.cpp file corresponds to a compilation unit. Subsequently, the compiler (CC) translates each compilation unit into a local binary code, and the suffix in gnu/linux is. O, suffix. obj in Ms VC. This binary code, also known as the object file, has code, data, heap, STATCK, and so on as the final destination file, but it cannot be run (missing entry points). The linker (LD) locates multiple object files in the target file, which is the process of relocate, adding additional code (other linked files) and eventually generating the target file. The destination file can be an executable file, a static link file, or a dynamically linked file.
1. The static link file in the Gnu/linux system is actually a compressed package of multiple. o files. Suppose we have cool.h cool.c and some.c files to get the static link library LIBCOOL.A. First use the following instructions to get the corresponding object file COOL.O and SOME.O:
Gcc-c COOL.C
Gcc-c some.c
The object file generated in this way is called the PDC, position-related code (position-dependence). Use the following instructions to get the static link file Libcool.a:
Ar-r LIBCOOL.A COOL.O SOME.O
Ranlib LIBCOOL.A
Static link library LIBCOOL.A compliance with gnu/linux-defined static link library Naming conventions must be "LIBYOUR_LIBRARY_NAME.A"

2. Dynamically linked files in Gnu/linux must be generated through the linker LD. Suppose we have hot.c other.c and other files to generate the dynamic link library libhot.so. First use the following instructions to get the corresponding object file HOT.O and SOME.O
    gcc-fpic-c hot.c
    gcc-fpic-c other.c The
parameter-fpic specifies that the generated object file is position-independent (Position-independence code), and only PIC can be used as a build dynamic-link library. Then use the following instructions to get the dynamic library:    
    ld-bshared-o libhot.so hot.o other.o
or the LD that can use the compiler WRAPPER:  &NBSP
    gcc-shared-o libhot.so hot.o other.o You can also use the compiler to generate dynamic libraries directly:
     gcc-fpic-shared-o libhot.so hot.c other.c
Here the option-shared indicates that the type of the destination file is a dynamic link library, and the naming specification for the dynamic library is "Libyour_library_ Name.so "

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.