Create and use files in the library in linux and other

Source: Internet
Author: User
Article Title: Create, use, and other library files under linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

First, we will explain the differences between the header file, library file, and library function concepts. Header file: A file suffixed with. h, in which variables and function prototypes are declared. Library File: Provides files with library functions. One is a file with the extension of. a for static connections, which is connected during program compilation; the other is a shared library with the extension of. so, which is connected only when the program is running. Library Function: The function provided in the library file.
Next, we will use an instance to introduce how to create library files and how to apply library files.
File 1: mymath. h
# Ifndef MYMATH_H
# Define MYMATH_H
Int xiangjia (int a, int B );
Int xiangjia (int a, int B );
# Endif
File 2: xiangjia. c
Int xiangjia (int a, int B)
{
Return a + B;
}
File 3: xiangjian. c
Int xiangjian (int a, int B)
{
Return a-B;
}
File 4: main. c
# Include "mymath. h"
# Include "stdio. h"
Int main ()
{
Printf ("% d", xiangjia (65,45 ));
Printf ("% d", xiangjian (65,45 ));
Return 0;
}
The. o file is required for both dynamic and static databases. The. o file is compiled and generated first. o file.
# Gcc-c xiangjia. c xiangjia. c main. c

1. Create a static database
The naming rules for static library file names are prefixed with lib, followed by the static library name and the extension is.. For example, if we name the created static library math56, the static library file name is libmath56.a.
# Ar cr libmath56.a xiangjia. o xiangjian. o
Link the static library to generate an executable file:
# Gcc? O math main. o libmath56.a
Or # gcc? O math main. c? L .? Lmath56
Run:
#./Math
110
20

Ii. Use the. o file to create a dynamic library
The file extension of the dynamic library is. so. Use the following command to generate the l ibmath56.so file.
# Gcc-shared-fPCI-o libmath56.so xiangjia. o xiangjia. o
Generate the executable file math2
# Gcc? O math2 main. c-L .? Lmath56
Run:
#./Math
./Math: error while loading shared libraries: libmath56.so: cannot open shared object file: No such file or directory
Oh, an error occurred! Check out the error message. The shared library file libmath56.so is not found. 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, a message similar to the preceding error will be prompted to terminate the program.
There are three ways to find the generated dynamic library:
1) copy the database to the/usr/lib and/lib directories.
(2) Add the path of the library in the LD_LIBRARY_PATH environment variable.
For example, the dynamic library libhello. so is in the/home/example/lib directory:
$ Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH:/home/example/lib
(3) modify the/etc/ld. so. conf file, add the path of the Library to the end of the file, and execute ldconfig refresh. In this way, all the library files under the added directory are visible.
When the static library and the dynamic library have the same name, the gcc command takes precedence over the dynamic library.

3. Several Parameters
Readers may have forgotten the significance of the gcc compilation parameters used above. Here is a brief introduction:
-FPIC generates code irrelevant to the location, so that the Library can be linked and loaded at any location.
-Shared Connection Library generation
-Static: Specifies to generate a static Link Library.
-L specify the connected Library File
-L specify the location of the Library File
In linux, most functions place header files in the/usr/include directory by default, while in/usr/lib, this is not the case in all cases. When using C language to develop applications in Linux, there are few cases where no third-party function libraries are used, generally, one or more function libraries are needed to complete the corresponding functions. Because of this, gcc must let the compiler know how to find the required header files and library files during compilation. Gcc uses the Directory Search method to find the required files. The-I option can add a new directory to the gcc header file search path. For example, if the header files required for compilation are in the/home/david/include directory, you can use the-I option to make gcc find them smoothly:
# Gcc david. c? I/home/david/include? O david
Similarly, if you use a library file that is not in the standard location, you can use the-L option to add a new path to the gcc library file search path, for example, in the/home/david/lib directory, you can use the-L option to find the library files required for compiling:
# Gcc david. c? L/home/david/lib? Ldavid? O david
Here I will explain the-l option, which instructs gcc to connect to the library file libdavid. so, there is a convention in naming the library files in linux, that is, it starts with lib, and all the library files follow this rule, therefore, when you use the-l option to specify the connected library file, you can omit the lib three characters.

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.