Static libraries and dynamic libraries under Linux

Source: Internet
Author: User

The library, in essence, is a library function that we use when we program, so we can avoid repeating this code. such as: standard input and output functions in C/s + +, we are using their library;

The static library is when we compile our functions, directly to the library to be used to compile the content of the inclusion, so that they become one, and the dynamic library, we compile our function is not directly to use the contents of the library to be included in, but to run the time to call it, as for their shortcomings, It is clear to think with your mind.

Static libraries:

Use. A as suffix, meaning archive file; Static library is obtained by command ar; command ar to pre-process, compile, and compile the source code. o files can be contained by multiple. o files;

For example, when we write the C program, it will default to include the C standard library/USR/LIB/LIBC.A, which has ans1/iso standard specified functions, such as printf. So we do not have to manually link, if you use a function other than the standard specified function, then we have to manually link the relevant library, such as the Mathematical Static library Libm.a (header file is math.h)

Command of AR:

later supplemented;

/****/

Example: Write a libhello.a static library below, and then call through the MAIN.C function;

Writing hello.h

void hello ();

Writing hello.c

#include <stdio.h>"hello.h"void  hello () {                                                                          printf ( " Hello, world\n. " );}

Generate hello.o Target file

gcc -o hello.o-c hello.c

Generate LIBHELLO.A Static Library

ar -cqs libhello.a hello.o

Edit Main.c File

" hello.h " int Main (intChar* * argv) {    hello ()    ; return 0 ;}

Now use the static library to generate the final executable code for the MAIN.C:

Gcc–o out MAIN.C libhello.a

Run the code to see the results:

./ Outhello,world!

Dynamic libraries:

Static libraries and dynamic libraries under Linux

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.