02.GCC Getting Started (bottom)

Source: Internet
Author: User

◊ Basic framework:

A) use of external libraries

b) The difference between a static library and a shared library

c) generation of static libraries

D) generation of shared libraries

2.1 use of external libraries

Features of the 2.1.1 library file:

1) The library file is actually some. o file packaging;

2) is prepared in accordance with the principle of reusable;

3) usually consist of a set of interrelated functions;

4) The maintainability of the software is good, if the software upgrade, usually only need to modify the library code, rather than modify the use of the library code (as long as the use of the library's interface has not changed).

2.2. System storage location for 2 header files and library files

Header file:/usr/include/usr/local/include

Library files:/usr/lib/usr/local/lib

2.2.3 Use of external libraries

-lname means to link libname.so or libname.a library files

If both the static library and the shared library exist, the system will prioritize the shared library.

2.2 the difference between a static library and a shared library

Features of the Static library (. A):

1) The program links to the required library in the corresponding function code link to the executable file;

2) No need for static library when running;

3) If more than one program uses the same library, it requires multiple copy of the relevant code in the library, so it is expensive for memory and disk space.

Features of the shared library (. So):

1) When the program runs, the library code is loaded from disk into memory by the operating system, which is called dynamic link;

2) The existence of shared libraries is required when running;

3) If multiple programs use the same shared library, then when the program runs, just load a copy of the library code to the memory, multiple programs share the same library;

And because the executable program contains only one table of the function entry address that it uses, not a copy of the library code;

So it saves memory and disk space;

4) because there is no relevant copy of the library code in the executable program, the maintenance performance is good.

2.3 generation of static libraries

2.3.1 Generation of static libraries

Ar RCS Libname.a NAME.O

RCS representation (replace, create, save)

Ar T queries how many. o files are in a static library

Search path for 2.3.2 library:

1) Search from left to right for –i–l specified directory; ( preferably using the 1 method to set)

2) The directory specified by the environment variable

Setting method: Add a search path to the corresponding environment variable in the configuration file (~/.bash_profile):

Export C_include_path= ...

Export Cpp_include_path= ...

Export Library_path= ...

You can then execute the configuration file.

3) system default search directory

/usr/include/usr/lib

2.4 generation of shared libraries

2.4.1 Generation of shared libraries

Gcc–shared-fpic Hello.o–o libhello.so

FPIC: Used to generate location-independent codes (position independent code)

2.4.2 Some of the configurations required to run a shared library (2 methods)

1) Copy the. So file to the System shared library path:/usr/lib

2) Set environment variable: ld_library_path ( preferably using the second method )

2.4.3 LDD + executable file name: query which shared libraries the program needs to load at execution time.

02.GCC Getting Started (bottom)

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.