How to compile dynamic and static libraries in C language ?, Static Compilation

Source: Internet
Author: User

How to compile dynamic and static libraries in C language ?, Static Compilation

The role of dynamic and static libraries is not described in detail here. It focuses on how to compile and use dynamic and static libraries. Some large programs generally have a good architecture, various functional modules can coordinate with each other to achieve the expected results, but some programs do not write their own source code, especially the libraries provided by chip manufacturers; the libraries to be provided are the libraries that can be integrated;

Raw Material: test1.c test2.c main. c

(The detailed code is not listed in detail. Everyone is a developer and wants to find the desired item in the shortest time, instead of wasting too much time on the details)

1. How to compile a dynamic library

Gcc test1.c test2.c-shared-fPIC-o libtest. so

Use Dynamic library

Gcc main. c-L.-ltest-o a. out

(

-L: indicates the path of the required database.

-L: indicates the name of the library, for example, libtest. so. The name is test.

)

(Ps: execute. libtest cannot be found. so file, you need to put the library file under the/lib directory, or add the environment variable LD_LIBRARY_PATH, including the path of the library file)

2. How to compile a static library

Gcc-c test1.c test2.c

Ar-r libtest. a test1.o test2.o

Use static library

Gcc main. c-static-L.-ltest-o a. out

(

-Static: the static library can be used for forced compilation. If this parameter is not used, and the static library has the same name as the dynamic library, the dynamic library will be used first.

)

3. Static and Dynamic Databases

When a static library is used for compiling executable program execution conditions, the generated execution file is no longer dependent. a static library file, and the execution file compiled using the dynamic library must contain. so library files can be run;

In terms of the execution speed of executable programs, static compilation files are better than dynamic compilation (after all, static compilation compiles the code of library files to the execution files, different from dynamic compilation, the link is searched for in the dynamic library in the path only when the function in the dynamic library is executed );

In terms of the size of executable programs, the size of static compiled files is much larger than that of dynamic compiled files. If the memory requirements are high, you can use dynamic compilation, although it will be a little slower;

After modifying the library, the static library and the original main must be added to the static compilation file. c files can only be used after being re-compiled, but the dynamic compilation of files does not need to be compiled again. c. You only need to overwrite the original dynamic library with the newly compiled dynamic library, and then re-run the executable file to take effect. It takes an hour or two to compile a large project, the advantages of the dynamic library do not need to be repeated;

Related Article

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.