Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and use linux static Library and dynamic Library

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the creation and use of linux static library and dynamic library". In daily operation, I believe many people have doubts about the creation and use of linux static library and dynamic library. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about the creation and use of linux static library and dynamic library. Next, please follow the editor to study!

Creation and use of static Library and dynamic Library

1. Basic concept

Static library: when linked, the library is packaged into an executable file along with the compiled .o file. The extension under linux / windows is: .a / .lib.

Dynamic library: when linked, it is not packaged into an executable file, and the dynamic library is loaded dynamically when executed. The extension under linux / windows is: .so / .dll.

The differences are mainly reflected in:

A. maintainability.

Join the update. Merge the updates of the dynamic library, just replace the library. But merging into static requires re-linking to generate a new executable program with full updates.

Environmental dependence. If a static library is included, it does not depend on the environment after compilation. On the other hand, there is a dependency relationship in the dynamic library, which requires the corresponding dynamic library in the environment.

B, size

Space utilization. If you rely on the same static library, there are multiple copies.

Executable file size. The static library file is larger.

C. Dynamic library can realize resource sharing between processes, so it is also called shared library.

two。 Creation and use of static Library

A static library is essentially a set of .o compressed files with a .a / .lib extension.

The code is as follows:

Ar-crv libXXX.a mmmm.o nnnn.o

XXX is the library name, and mmmm.o nnnn.o is the target file to be packaged. Parameter crv, which means

C: do not warn if the library had to be created

R: replace existing or insert new file (s) into the archive

V: be verbose

When using, simply introduce the header file in the code, and when linked, specify the search path of the static library (- L option) and the static library name (no lib prefix and .a suffix,-l option is required).

The code is as follows:

Gmail + test.cpp-L../StaticLibrary-lXXX

3. Dynamic library

The execution file format of Window is different from that of Linux, and there are some differences when creating dynamic libraries.

The execution file under Windows is in PE format, and the dynamic library needs an entry for initializing the DllMain function. Usually, the _ declspec (dllexport) keyword is required when exporting the declaration of the function.

The execution file compiled by gcc under Linux is in ELF format by default, and there is no need to initialize the entry or make a special declaration of the function, so it is convenient to write.

The naming format of dynamic library under linux is the same as static library, which is libXXX.so format.

Dynamic libraries can be created using compilers, without the need for specialized packaging tools like static libraries.

To create a dynamic library, add the-fPIC option when the source code is compiled and the-shared option when linking.

The code is as follows:

Gmail +-fPIC-shared-o libXXX.so xxx.cpp

Usage is the same as static library

The code is as follows:

Gmail + test.cpp-L../DynamicLibrary-lXXX

You just need to set up the search path. The default search path is / lib/, / usr/lib.

Set the search path:

Edit / etc/ld.so.conf, set the search path

Run ldconfig to rebuild the / etc/ld.so.cache file. The setting takes effect.

4. Related tools

Ldd view shared libraries (dynamic libraries) on which the program depends

The code is as follows:

Gcc / gathers + parameters-I (uppercase'i'),-L (uppercase'l'),-l (lowercase'L')

1.-I (uppercase'i') header file directory

2.-L (uppercase'l') library directory

3.-l (lowercase'L') library name

Uppercase is the directory, lowercase is the library.

At this point, the study on "the creation and use of linux static and dynamic libraries" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report