Create a linux Library File

Source: Internet
Author: User
Article Title: Create a linux library file. 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.

I. Why should I use library files?

We will certainly encounter this situation in actual programming: There are several projects with the same function modules,

The implementation code is also the same, which is what we call repeated code. For example, many projects have a user verification function.

The code snippet is as follows:

// UserLogin. h file, providing function declaration

Int IsValidUser (char * username, int namelen );

// The UserLogin. c file to verify user information

Int IsValidUser (char * username, int namelen)

{

Int IsValid = 0;

/* The following is the specific processing code, skipped */

Return IsValid

}

If each project stores the two UserLogin. h and UserLogin. c files

Disadvantages:

1. duplicate modules exist in each project, resulting in code duplication.

2. code reusability is poor. Once the IsValidUser code changes, in order to maintain design consistency,

We also need to manually modify the UserLogin. c file in other projects, which is time-consuming, laborious, and error-prone.

Library files are an organizational form of public code.

In order to solve the above two drawbacks, we propose a solution to store public code with library files. The main point is

The public (that is, the target code that can be reused multiple times) is separated from the project and stored in the library file in a unified manner,

When the project uses the code, it can obtain the target code from the library file during compilation or running. Library files

There are two types: static library and dynamic library.

Ii. Static and Dynamic Databases

If the program loads the library file during compilation, the static library is used. If the target code is loaded at runtime,

It becomes a dynamic library. In other words, if a static library is used, the static library code is copied to the code segment of the program during compilation,

The size of the program expands. If a dynamic library is used, only the name and function name of the library file are retained in the program.

Library files and function bodies, the size of the program has not changed much.

The principle of static databases is to change the space for time to increase the program volume and reduce the running time;

The dynamic library is "Time for Space", which increases the running time, but reduces the size of the program itself.

The following is an example of how to use these two libraries.

Iii. Writing and using static databases

1. Overview

The extension of the static library file is generally. a, which is easy to write.

(1) Compile Function Code

(2) compile and generate target files

(3) archive the target file with an ar file to generate a static library file.

Note that the archive file name must start with lib.

Usage tips:

(1) Add the path of the static library header file after the-I parameter of gcc.

(2) Add the directory of the library file after the-L parameter of gcc

(3) Add the library file name after the-l parameter of gcc, But remove the lib and. a extensions.

For example, if the library file name is libtest. a, the parameter is-l test.

2. Write the simplest static library file

Write the following two files and place them in the same directory.

Myalib. h // static library header file

Myalib. c // static library implementation file

// Myalib. h file content

Void test ();

// Myalib. c file content

# Inlcude

Void test ()

{

Printf ("test \ n ");

}

3. Create a library file

(1) generate the target file

Gcc-c myalib. c

A myalib. o file is generated after execution.

(2) Use the ar command for archiving. The format is ar-rc. <生成的档案文件名> <. O file name list>

Again, the archive file name must start with lib and end with..

Ar-rc libtest. a myalib. o

A libtest. a file is generated after execution.

4. Use library files

(1) Compile a Test Program main. c. The content is

// Main. c test the program called by the static library

# Include "myalib. h" // include the function header file. Otherwise, an error is reported during compilation.

Int main (int argc, char * argv [])

{

Test ();

Return 0;

}

(2) Compile the target file. Add the path of the static library header file to the-I parameter.

Gcc-I/root/exercise-o main. o-c main. c

A main. o file is generated.

(3) generate an executable file. Add the path of the static library file to the-L parameter,

Add the library file name (excluding the hitting lib and ending. a) to the end of the-l parameter. As shown below

Gcc-o main-L/root/exercise main. o-ltest

An executable file named main is generated.

In addition, note that the-l parameter should be added to the end of the input file name; otherwise, an error is returned.

For example, gcc-o main-L/root/exercise-ltest main. o will prompt

Main. o (. text + 0x11): In function 'main '':

: Undefined reference to 'test''

Collect2: ld returned 1 exit status

The reason is unknown :-)

(4) display the executable file

Run./main and Output

Test

The execution is successful.

[1] [2] [3] Next page

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.