How to Create a function library in Linux

Source: Internet
Author: User
In Linux, how to create your own function library-general Linux technology-Linux programming and kernel information is described below. I want to create a function library and include some of my frequently used functions. I just need to include them when using them. How should I build them? In addition, I see some commonly used function libraries, such as math. h, which only contain some function declarations. Where is the execution part? How is the call implemented? Programming for beginners. Thank you!

Something I wrote before. It may be useful to you!

How to add a library for Linux

I. Static Library
In Linux, the static library is a file suffixed with..
1. Create a static database
H1.c source file
# Include
Void hello1 ()
{
Printf ("the first hello! \ N ");
}
H2.c source file
# Include
Void hello2 ()
{
Printf ("the second hello! \ N ");
}
2. Main Program
Hello. c source file
# Include
Int main ()
{
Hello1 ();
Hello2 ();
Return 0;
}
Enter the following command:
Gcc? C h1.c
Gcc? C h2.c
Ar? R libhello. a h1.o h2.o
Ar? S libhello.
Ranlib libhello.
Last
Gcc? Static hello. c? L .? Lhello? O hello, you can generate an executable file. Note that you must use the-static parameter. Otherwise, the generated files are still dynamic files. However, for the hello database, static files are used.

Ii. Dynamic library
1. Create a dynamic library
# Include
Void hello1 ()
{
Printf ("the first hello! \ N ");
}
H2.c source file
# Include
Void hello2 ()
{
Printf ("the second hello! \ N ");
}
2. Main Program
Hello. c source file
# Include
Int main ()
{
Hello1 ();
Hello2 ();
Return 0;
}
Enter the following command:
Gcc? FPIC? G? C h1.c? O libh1.o
Gcc? FPIC? G? C h2.c? O libh2.o
Gcc? G? Shared? W1? O libh1.so libh1.o? Lc
Gcc? G? Shared? W1? O libh2.so libh2.o? Lc
Then, the main program is connected to the library for compilation.
Gcc? G hello. c? O hello? L .? Lh1? Lh2
Finally, put the current path in the database search path.
Export LD_LIBRARY_PATH =.: $ LD_LIBRARY_PATY
Run./hello.

3. Search for dynamic Databases
Once the connector completes initialization, you can find the name of the library required by the program. The program header has a pointer pointing to the dynamic segment, which contains dynamic connection information. The DT_STRTAB in the dynamic segment points to a two-dimensional table, and the DT_NEEDED contains an offset relative to the two-dimensional table, the name of the database.
For each database, the connector first looks for the location of the database file, which is essentially a complicated process. The library file name described in DT_NEEDED is generally similar to libXt. so.6 (Xt Development Kit, version 6). The library file may be in any library file directory, and may contain duplicate files. In my system, the actual file name of this library is/usr/X11R6/lib/libXt. so.6.0, And the last ". 0" indicates the version number.

The connector looks for the following:

L first, check whether the. dynamic segment contains an item named DT_RPATH (which is a list of search directories separated by colons ). This item is added by the command line switch or environment variable when the program is connected by a connector. It is often used in subsystems. For example, for database applications, we need to load some Program sets and support databases to a directory.

L check whether the environment variable LD_LIBRARY_PATH exists (it is a list of library file search directories separated by colons ). This item helps developers create a new version of library, add its path to LD_LIBRARY_PATH, and use it with existing connectable programs to test new libraries,

L connector to view the library cache file/etc/ld. so. conf, which contains a list of database names and paths. If the database name exists, the connector uses the corresponding path, you can use this search method to find most libraries (the file name does not need to be exactly the same as the requirements. For details, refer to the following "database version ").

L If none of the above searches fail, the connector searches for the default path/usr/lib. If the library file is still not found, an error is displayed and then exits.

After the connector finds the library file, open it first, read the ELF header, and find the pointer pointing to each segment. The connector allocates space for library code segments and data segments and maps them to the memory, followed by bss (no space allocated ).. Through the. dynamic segment of the library, the connector adds the symbol table of the Library to the symbol table chain. If other libraries on which the library depends are not loaded, add the Library to the load queue.

After this process is completed, all databases have been mapped. loader logically has a global symbol table, which is the union of all programs and the symbol table of the mapped database.
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.