Linux static library and dynamic library 20160706

Source: Internet
Author: User
Tags naming convention

A static link is a link to a function or procedure to be called into an executable file, as part of an executable file. When multiple programs call the same function, multiple copies of the function are present in memory, wasting valuable memory resources.. so files are shared library files (dynamic links). The function code called by the dynamic link is not copied to the application's executable file, but is simply added to the description of the called function (often some relocation information), only when the application is loaded into memory to run, under the management of the operating system, To establish a link relationship between the application and the corresponding. So.

A. A file is a combination of multiple. o Files: An o file is an object file that contains content that is a 01 machine executable instruction that needs to be linked when the program is executed. The link is to chain multiple. o files into an executable file.
About Library Generation issues
We usually make some common functions into libraries for other programs to use. function library is divided into static library and dynamic library two kinds. The static library is connected to the target code when the program is compiled, and the static library is no longer needed when the program runs. Dynamic libraries are not connected to the target code when the program is compiled, but are loaded only when the program is running, so a dynamic library exists when the program is run.
(1) Static library
Simply put, a static library is a simple collection of target files. Therefore, the first thing to do is to resolve the target file.
The first step: Compile the source file where each function code is located into a catalog file.
For example, for MYFUNC.C, MYPROC.C
Gcc-c MYFUNC.C MYPROC.C
Will get MYFUNC.O and MYPROC.O.
Step two: Set up multiple target files by AR (archive, archived).
$ar-R LIBMYJOB.A MYFUNC.O MYPROC.O
In general, static libraries should be named in the LIBXXXXX.A format. When using a static library, the application usually needs to pass the xxxxx part of the naming to GCC. For example:
$GCC –o Mywork–lmyjob ...
The idea is to have gcc (actually GCC calls LD) connect to a library named LIBMYJOB.A (or libmyjob.so). If the name of the library does not follow the LIBXXXXX.A format, the corresponding file cannot be found.
Example: Creating a static Library
Hello.h is the header file for the function library. HELLO.C is the source program for the library, which contains the public function hello, which will output on the screen "
Hello xxx! ". MAIN.C is the main program for the test library file, and the public function Hello is called in the main program.
Program 1:
Hello.h
#ifndef Hello_h
#define Hello_h
void Hello (const char *name);
#endif
Program 2:
hello.c
#include <stdio.h>
void Hello (const char *name)
{
printf ("Hello%s! \ n ", name);
}
Program 3:
Main.c
#include "hello.h"
int main ()
{
Hello ("everyone");
return 0;
}
Implementation steps:
The first step: the source program must be hello.c through GCC first compiled into. o files, generate hello.o (Static library/dynamic library, all created by. o files);
Step two: Create a static library from the. o file, generate LIBMYHELLO.A (the naming convention for Static library filenames is the LIB prefix, followed by the static library name, with the extension. A) to create a static library with AR command;
The third step: use static libraries in your program, (just include the prototype declarations for these common functions in the source program that uses them, and then specify the static library name when you generate the target file with the GCC command, and GCC will connect the public functions from the static library to the destination file.) Notice that GCC adds the prefix lib to the static library name and then appends the extension. A to the static library file name to find the static library files)
Fourth step: Delete the static library file, the program runs as usual, the public function in the static library Hello has been connected to the target file in main.
Run:
[[email protected] moduletest]# ls
HELLO.C hello.h MAIN.C
[Email protected] moduletest]# gcc-c hello.c
[[email protected] moduletest]# ls
HELLO.C hello.h hello.o main.c
[email protected] moduletest]# ar crv libmyhello.a hello.o
a-hello.o
[[email protected] moduletest]# ls
hello.c hello.h hello.o libmyhello.a main.c
[Email protected] moduletest]# gcc main.c libmyhello.a-o Main
[Email protected] moduletest]#./main
Hello everyone!
[Email protected] moduletest]# rm-f LIBMYHELLO.A
[[email protected] moduletest]# ls
HELLO.C hello.h hello.o Main MAIN.C
[Email protected] moduletest]#./main
Hello everyone!
[Email protected] moduletest]#
(2) Shared library
The construction of a shared library is a bit more complex, usually an elf-formatted file. There are three ways to generate:
$ld-G
$GCC-shared
$libtool
Using LD is the most complex, with Gcc-share is much simpler, but-share is not available on any platform. GNU provides a better tool for Libtool, which is designed to generate a variety of libraries on a variety of platforms.
Using GCC's-shared parameter:
Gcc–shared–o libmyjob.so MYJOB.O
In this way, the shared library file libmyjob.so is generated through MYJOB.O.
In particular, in a Cygwin environment, you still need to output a shared library (dynamic library) that matches Windows naming, or LibXXXXX.dll. Such as:
Gcc–shared–o Libmyjob.dll MYJOB.O
Example: Creating a dynamic library (with the program above)
Implementation steps:
Fifth step: Create a dynamic library file from an. o File (command: Gcc-shared-fpci-o libmyhello.so hello.o);
Sixth step: Using dynamic libraries in programs, (using dynamic libraries in programs and using static libraries is exactly the same as prototyping declarations that include these common functions in source programs that use these common functions, and then specifying the dynamic library name to compile when generating the target file with the GCC command.) At run time, the program looks for the required dynamic library files in directories such as/usr/lib and/lib. If found, loads the dynamic library, otherwise it will prompt the error message and terminate the program. To copy the file libmyhello.so to the directory/usr/lib)
Run:
[[email protected] moduletest]# ls
HELLO.C hello.h hello.o main.c
[Email protected] moduletest]# Gcc-shared-fpic-o libmyhello.so hello.o
[[email protected] moduletest]# ls
hello.c hello.h hello.o libmyhello.so main.c
[Email protected] moduletest]# gcc main.c libmyhello.so-o Main
[[email protected] moduletest]# ls
HELLO.C hello.h hello.o libmyhello.so main MAIN.C
[Email protected] moduletest]#./main
./main:error while loading shared libraries:libmyhello.so:cannot open Shared object file:no such file or directory
[Email protected] moduletest]# MV Libmyhello.so/usr/lib
OK:
[[email protected] moduletest]# ls
HELLO.C hello.h hello.o Main MAIN.C
[Email protected] moduletest]#./main
Hello everyone!
[Email protected] moduletest]#
Or:
[Email protected] moduletest]# rm-f main
[[email protected] moduletest]# ls
HELLO.C hello.h hello.o main.c
[Email protected] moduletest]# gcc-wall-g main.c-lmyhello-o Main
[[email protected] moduletest]# ls
HELLO.C hello.h hello.o Main MAIN.C
[Email protected] moduletest]#./main
Hello everyone!
[Email protected] moduletest]#
Attention:
When the static library and the dynamic library have the same name, the GCC command takes precedence over the dynamic library.
(3) Future configuration of library generation
If you want to install the library files you have developed into the operating system, you need Administrator privileges:
(a) Copy the library files to the appropriate directory:
You can put your own dynamic connection libraries into/usr/local/lib (or/usr/lib), or put them in other directories, but wherever they are, they must match the values of Library_path, Ld_library_path.
(b) Modify the relevant system configuration file:
Modify the/etc/ld.so.conf and then use/sbin/ldconfig to complete.
Note:
Compile parameter resolution
The main thing is an option for the GCC command line:
-shared This option specifies to generate a dynamic connection library (let the connector generate the export symbol table of type T, and sometimes the export symbol of the weakly connected W type) without which the external program cannot connect. Equivalent to an executable file
L-fpic: represents compiled to a location-independent code, without this option, the compiled code is location-dependent, so dynamic loading is a way of copying code to meet the needs of different processes, but not to achieve the purpose of real code segment sharing.
L-l.: Indicates the library to be connected in the current directory
L-ltest: The compiler has an implicit naming convention when looking for dynamic connection libraries, which is to precede the given name with Lib, followed by. So to determine the name of the library
L Ld_library_path: This environment variable indicates that the dynamic connector can load the path of the dynamic library.
L Of course, if you have root permissions, you can modify the/etc/ld.so.conf file, and then call/sbin/ldconfig to achieve the same goal, but if you do not have root privileges, you can only use the output Ld_library_path method.
Call the dynamic library when there are several problems often encountered, and sometimes, clearly has the library's header file in the directory through the "-I" include, the library is located in the file through the "-l" parameter boot, and specified the "-L" library name, but through the LDD command to see, it is dead and alive can not find your designated link , you have to specify the directory of the dynamic library by modifying the Ld_library_path or/etc/ld.so.conf file. This is usually done to solve the problem where the library cannot be linked.

Linux static library and dynamic library 20160706

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.