Static and dynamic libraries generated by GCC

Source: Internet
Author: User

Static and dynamic libraries generated by GCC

I. Introduction to library files

To put it simply, a library is a set of functions and variables that have been written and compiled code. It is designed to improve development efficiency and running efficiency. Libraries are classified into static libraries and shared libraries. The extension of the static library file is. A, and the extension of the shared library file is. So (in the cygwin environment, they are called. O and. dll respectively ). A Shared Library is often called a dynamic library because many people borrow the word "Dynamic Linked Library" from Microsoft Windows.
(1) static library
Static means that each application that uses this database has a copy of its own database. When an application is running, it is okay to delete the database, because the application already has its own copy.
(2) Shared Library
A shared library may be shared by multiple applications. Therefore, every application should not delete a shared library even if it is no longer used. In addition, the application needs to set the correct environment variable (LD_LIBRARY_PATH) to locate the location of the shared library. Otherwise, the application reports that the library cannot be found when the application is running.

Ii. database usage Problems

If the library has been compiled, how can I use it when developing and running applications? The path of the header file and library file must be notified to the compiler, linker, and related applications in an appropriate way.
Static libraries mainly involve development tools, such as GCC. For example, when compiling and linking with GCC, you need to find the header file and static library file through the appropriate path. There are two methods to achieve this:
GCC command line parameters (-I,-l)
Shell environment variables (c_include_path, LIBRARY_PATH
For Shared libraries, if a program uses a dynamic library during runtime, you also need to find the corresponding dynamic library file. Implementation Method:
Shell environment variable (LD_LIBRARY_PATH)
1) GCC command line parameters (-I,-l)
By default, GCC will automatically search for the following path:
First file:
/Usr/local/include/
/Usr/include/
Database file:
/Usr/local/lib/
/Usr/lib/
However, because the system administrator has different configurations for the system installation path, or for a 64-bit system, the above paths may be different for a specific computer. If the developer still has the header files and library files required by the project, use the-I and-l of GCC to specify the corresponding path. If you need to link the library, you must use the-L option.
For example, if the project involves the gdbm (GNU Database Management) package, the libgdbm library is required, and the path for installing gdbm in the system is:
Header file:/opt/gdbm-1.8.3/include
Library File:/opt/gdbm-1.8.3/lib/
The GCC command parameters are:
$ GCC... -I/opt/gdbm-1.8.3/include-L/opt/gdbm-1.8.3/lib-lgdbm
Note: To ensure compatibility, you must resolutely put an end to using the above path in the # include statement of the C/C ++ source file or other related statements.
2) Shell environment variable (environmental variable)
Besides using command line parameters, you can also use environment variables to indicate GCC to search for appropriate paths. The setting of environment variables varies depending on shell. Common shells include bash, CSH, and tcsh.
(1) bash
For Bash, in addition to the content configured by the system administrator, each user's user directory ($ home) has a. bash_profile file. You can add the following two statements to the file to set the environment variables of the gdbm header file path:
C_include_path =/opt/gdbm-1.8.3/include
Export c_include_path
Similarly, use the following two statements in the file to set the environment variables of the library file path:
Library Path =/opt/gdbm-1.8.3/lib
Export LIBRARY_PATH
After the preceding statement is available in. bash_profile, you do not need to use-I and-l to search for the path of a specific package. However, you must use the-L option when linking the database.
$ GCC... -Lgdbm
In Bash, you need to check the environment variables and use the Env command.
$ ENV
(2) CSH and tcsh
For CSH or tcsh, the environment variables are set differently. In the user's ($ home) directory, the related files are as follows:
. Cshrc start (startup) file every time it enters CSH
. Tcshrc start (startup) file every time it enters tcsh (in tcsh, if this file is not available, the system will replace it with the. cshrc file)
. Login the startup (startup) file for each shell Login
CSH and tcsh are divided into shell variables and environment variables. The former is used to set the shell itself, and the latter is used by other programs. The general habit is: Shell variables are defined in. cshrc, while environment variables are defined in the. login file.
The method for defining shell variables is to use the set statement in. cshrc or. tcshrc:
Set history = 20
To define environment variables, use the setenv statement in the. login file. For the above example about gdbm:
Setenv c_include_path/opt/gdbm-1.8.3/include
Setenv LIBRARY_PATH/opt/gdbm-1.8.3/lib
Under CSH and tcsh, you can use the setenv command to view which environment variables are set (if you want to view shell variables, use the set command ).
Note:
-"=" Is used to set shell variables;
-When setting environment variables, there is no "=" between the variable name and the actual value (the actual path here;
-Export is not required.
3) use the Shared Library
For applications that use the shared library, find the corresponding shared library file through the Environment Variable LD_LIBRARY_PATH. Like other environment variables, LD_LIBRARY_PATH must also be set based on the shell type and the actual path of the library file. However, it must be noted that, unlike the general environment variables, the value of LD_LIBRARY_PATH is the path of all the shared libraries that have been installed. Therefore, in bash, you cannot simply use the following method:
LD_LIBRARY_PATH =/opt/gdbm-1.8.3/lib error!
Export LD_LIBRARY_PATH
But must use:
LD_LIBRARY_PATH =/opt/gdbm-1.8.3/lib: $ LD_LIBRARY_PATH
Export LD_LIBRARY_PATH
In this way, the paths of other shared libraries are added together. Similarly, under CSH and tcsh,
Setenv LD_LIBRARY_PATH/opt/gdbm-1.8.3/lib: $ LD_LIBRARY_PATH

Iii. Database generation problems
We usually make some common functions into function libraries for use by other programs. Function libraries can be divided into static libraries and dynamic libraries. The static library will be connected to the target code during program compilation. This static library is no longer needed when the program is running. The dynamic library is not connected to the target code during program compilation, but is loaded only when the program runs. Therefore, dynamic inventory is required when the program runs.

(1)Static Library
In short, a static library is a simple set of target files. Therefore, the target file must be resolved first.
Step 1: Compile the source file of each function code into a directory file.
For example, for myfunc. C, myproc. c
Gcc-C myfunc. c myproc. c
Myfunc. O and myproc. O will be obtained.
Step 2: Combine multiple target files by Ar (archive.
$ Ar-r libmyjob. A myfunc. O myproc. o
Generally, the static library naming method should follow the libxxxxx. A format. When an application uses a static library, it generally only needs to pass the XXXXX section in the name to GCC. For example:
$ Gcc-O mywork-lmyjob...
Let GCC (in fact, GCC calls LD) to connect to a library named libmyjob. A (or libmyjob. So. If the library name does not follow the libxxxxx. A format, the corresponding file cannot be found.
Example: Create a static database
Hello. H is the header file of the function library. Hello. c is the source program of the function library, which contains the public function hello, which will be output on the screen"
Hello XXX! ". Main. c is the main program for testing the library file, and the public function hello is called in the main program.
Procedure 1:
// Hello. h
# Ifndef hello_h
# Define hello_h
Void Hello (const char * Name );
# Endif
Procedure 2:
// Hello. c
# Include <stdio. h>
Void Hello (const char * name)
{
Printf ("Hello % s! /N ", name );
}
Procedure 3:
// Main. c
# Include "Hello. H"
Int main ()
{
Hello ("everyone ");
Return 0;
}

Steps:
Step 1: You must first compile the source program Hello. c into a. o file through GCC to generate hello. O (both static and dynamic libraries are created by the. o file );
Step 2:. o file to create a static library and generate libmyhello. A (the naming rules for static library file names are prefixed with Lib, followed by the static library name and the extension is. a) use the AR command to create a static database;
Step 3: Use a static library in a program. (You only need to use the prototype declaration that contains these public functions in the source program that uses these public functions, then, specify the static library name when you use the GCC command to generate the target file. GCC will connect the public functions to the target file from the static library. Note: GCC will add the prefix lib to the static library name and append the static library file name with the extension. A to find the static library file)
Step 4: Delete the static library file and run the program as usual. The public function hello in the static library has been connected to the target file main.
Run:
[Root @ localhost moduletest] # ls
Hello. c hello. h main. c
[Root @ localhost moduletest] # gcc-C hello. c
[Root @ localhost moduletest] # ls
Hello. c hello. h hello. O main. c
[Root @ localhost moduletest] # ar CRV libmyhello. A hello. o
A-Hello. o
[Root @ localhost moduletest] # ls
Hello. c hello. h hello. O libmyhello. A main. c
[Root @ localhost moduletest] # GCC main. c libmyhello. A-O main
[Root @ localhost moduletest] #./main
Hello everyone!
[Root @ localhost moduletest] # rm-F libmyhello.
[Root @ localhost moduletest] # ls
Hello. c hello. h hello. O main. c
[Root @ localhost moduletest] #./main
Hello everyone!
[Root @ localhost moduletest] #

(2)Shared Library
The construction of the shared library is more complex. It is usually an elf file. There are three methods to generate:
$ LD-G
$ Gcc-shared
$ Libtool
Using LD is the most complex. using gcc-share is much simpler, but-share is not available on any platform. GNU provides a better tool, libtool, which is used to generate various libraries on various platforms.
Use the-shared parameter of GCC:
Gcc-shared-O libmyjob. So myjob. o
In this way, the shared library file libmyjob. So is generated through myjob. O.
In particular, in the cygwin environment, you still need to output a shared library (dynamic library) that complies with the Windows naming conventions, that is, libxxxxx. dll. For example:
Gcc-shared-O libmyjob. dll myjob. o

Example: create a dynamic library (extend the above program 1, 2, 3)
Steps:
Step 5: create a dynamic library file from the. o file (command: gcc-shared-fpci-O libmyhello. So hello. O );
Step 6: Use the dynamic library in the program. (using the dynamic library in the program is the same as using the static library, the source program that uses these public functions also contains the prototype declaration of these public functions, and then specifies the dynamic library name for compilation when the target file is generated using the GCC command. When the program is running, it searches for the required dynamic library files in the/usr/lib and/lib directories. If it is found, it is loaded into the dynamic library. Otherwise, an error message is prompted and the program is terminated. Copy the file libmyhello. So to the/usr/lib directory)
Run:
[Root @ localhost moduletest] # ls
Hello. c hello. h hello. O main. c
[Root @ localhost moduletest] # gcc-shared-FPIC-O libmyhello. So hello. o
[Root @ localhost moduletest] # ls
Hello. c hello. h hello. O libmyhello. So main. c
[Root @ localhost moduletest] # GCC main. c libmyhello. So-O main
[Root @ localhost moduletest] # ls
Hello. c hello. h hello. O libmyhello. So main. c
[Root @ localhost moduletest] #./main
./Main: Error while loading shared libraries: libmyhello. So: cannot open shared object file: no such file or directory
[Root @ localhost moduletest] # mv libmyhello. So/usr/lib
Yes:
[Root @ localhost moduletest] # ls
Hello. c hello. h hello. O main. c
[Root @ localhost moduletest] #./main
Hello everyone!
[Root @ localhost moduletest] #
Or:
[Root @ localhost moduletest] # rm-F Main
[Root @ localhost moduletest] # ls
Hello. c hello. h hello. O main. c
[Root @ localhost moduletest] # gcc-wall-G main. C-lmyhello-O main
[Root @ localhost moduletest] # ls
Hello. c hello. h hello. O main. c
[Root @ localhost moduletest] #./main
Hello everyone!
[Root @ localhost moduletest] #
Note:
When the static library and the dynamic library have the same name, the GCC command takes precedence over the dynamic library.

(3) configuration after library generation
If you want to install your own library files to the operating system, you must have the Administrator permission:
(A) copy the library file to the appropriate directory:
You can put the self-developed dynamic Connection Library in/usr/local/lib (or/usr/lib) or other directories, but no matter where it is, must be consistent with the value of LIBRARY_PATH and the value of LD_LIBRARY_PATH.
(B) modify the related system configuration file:
Modify/etc/lD. So. conf and use/sbin/ldconfig.

Note:
Compile Parameter Parsing
The most important option is the GCC command line:
-Shared this option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, and sometimes generate a weak connection W-type export symbol). External programs cannot connect without this sign. Equivalent to an executable file
L-FPIC: indicates the code compiled into a location independent, without this option, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, rather than truly sharing code segments.
L-L.: indicates that the database to be connected is in the current directory.
L-ltest: an implicit naming rule is used by the compiler to search for a dynamically connected database. That is, add lib before the given name and. So to determine the library name.
L LD_LIBRARY_PATH: The environment variable indicates the path where the dynamic connector can load the dynamic library.
L of course, if you have the root permission, you can modify/etc/lD. so. CONF file, and then call/sbin/ldconfig to achieve the same purpose. However, if you do not have the root permission, you can only use the LD_LIBRARY_PATH output method.
When calling a dynamic library, there are several problems that may occur frequently. Sometimes, the directory where the library header files are already included through "-I, the file where the library is located is guided by the "-l" parameter and the database name of "-l" is specified. However, when you run the LDD command to view the file, you cannot find the so file with the specified link, in this case, you need to modify LD_LIBRARY_PATH or/etc/lD. so. CONF file to specify the directory of the dynamic library. This usually solves the problem that the database cannot be linked.

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.