Create static and dynamic libraries in Linux

Source: Internet
Author: User

Original address: http://blog.chinaunix.net/space.php? Uid = 20543672 & Do = Blog & id = 94271

We usuallySome common functionsCreate a function library for otherProgram.

Function libraries can be divided into static libraries and dynamic libraries.

The static library will be connected to the target during program compilation.Code.

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.

This document provides examples to illustrate how to create static and dynamic libraries in Linux and how to use them.

Before creating a function library, we first prepare the source program for example and compile the source program of the function library into a. o file.

Step 2: edit the program named hello. H, hello. C, and Main. C;

Hello. H (see program 1) is the header file of the function library.

Hello. C (see Program 2) is the source program of the function library, which contains the public function hello, which will output "Hello XXX!" on the screen! ".

Main. C (see Program 3) is the main program of the Test Library file, and the public function hello is called in the main program.

Program 1: Hello. h # ifndef hello_h# DefineHello_hVoidHello (Const Char*Name );# Endif //Hello_h
 
Program 2: Hello. C # include<Stdio. h>VoidHello (Const Char*Name) {printf ("Hello % s! \ N", Name );}
Program 3: Main. C # include"Hello. h"IntMain () {Hello ("Everyone");Return 0;}

Step 2: Compile hello. c into a. o file;

Both static and dynamic libraries are created by the. o file. Therefore, we must first compile the source program Hello. c into a. o file through GCC.

Enter the following command at the system prompt to get the hello. o file.

# Gcc-C hello. c

#

(Note 1: This article does not introduce the usage of each command and Its Parameter functions. If you want to learn more about them, see other documents .)

(Note 2: the first character "#" is a system prompt and does not need to be typed. The following is the same .)

Run the LS command to check whether the hello. o file exists.

# Ls

Hello. c hello. h hello. O main. c

#

(Note 3: the first character "#" is the system running result, which is the same as the following .)

In the LS command result, we can see the hello. o file. This step is complete.

Next, let's take a look at how to create a static library and use it.

 

Step 2: Create a static library from the. o file;

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 the static library name is myhello, the static library file name is libmyhello.. Pay attention to this when creating and using static databases. Use the AR command to create a static library.

Enter the following command at the system prompt to create the static library file libmyhello..

# Ar Cr libmyhello. A hello. o

#

Run the LS command to view the result:

# Ls

Hello. c hello. h hello. O libmyhello. A main. c

#

The LS command result contains libmyhello..

 

Step 2: use static libraries in programs;

After the static library is created, how can I use its internal functions? You only need to include the prototype declaration of these public functions in the source program using these public functions, and then specify the static library name when generating the target file using the GCC command, GCC will connect the public functions to the target file from the static library.Note: GCC adds the prefix lib to the static library name, and appends the static library file name with the extension. A to find the static library file.

In Program 3: Main. C, we include the header file hello. H of the static library, and then directly call the public function hello in the main program. Run the hello program to check the result.

# Gcc-O hello main. c-L.-lmyhello

#./Hello

Hello everyone!

#

Let's Delete the static library file and try whether the public function hello is actually connected to the target file hello.

# Rm libmyhello.

RM: Remove regular file 'libmyhello. '? Y

#./Hello

Hello everyone!

#

The program runs as usual, and the public functions in the static library are connected to the target file.

Let's continue to see how to create a dynamic library in Linux. We start with the. o file.

 

Step 2: create a dynamic library file from the. o file;

The naming rules for dynamic library file names are similar to those for static library file names. The prefix Lib is also added for dynamic library names, but the file extension is. So. For example, if the dynamic library name is myhello, the dynamic library file name is libmyhello. So. Use GCC to create a dynamic library.

Enter the following command at the system prompt to obtain the dynamic library file libmyhello. So.

# Gcc-shared-fpci-O libmyhello. So hello. o

#

We still use the LS command to check whether the dynamic library file is generated.

# Ls

Hello. c hello. h hello. O libmyhello. So main. c

#

 

Step 2: Use the dynamic library in the program;

Using dynamic libraries in a program is exactly the same as using static libraries. It is also a prototype declaration that contains these public functions in the source program that uses these public functions, then, specify the dynamic library name for compilation when the target file is generated using the GCC command. Run the GCC command to generate the target file, and then run it to check the result.

# Gcc-O hello main. c-L.-lmyhello

#./Hello

./Hello: Error while loading shared libraries: libmyhello. So: cannot open shared object file: no such file or directory

#

Oh! Error. Check the error message. The dynamic library file libmyhello. 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. Copy the libmyhello. So file to the/usr/lib directory and try again.

# Mv libmyhello. So/usr/lib

#./Hello

Hello everyone!

#

Succeeded. This further demonstrates that the dynamic library is required when the program is running.

Let's look back and find that the GCC commands used to compile the static library and the dynamic library into the target program are exactly the same. When the static library and the dynamic library have the same name, which library file does the GCC command use? Let's take a look at the problem.

Delete all files except. C and. h and restore them to the example program State after editing.

# Rm-F hello. O/usr/lib/libmyhello. So

# Ls

Hello. c hello. h main. c

#

Create the static library file libmyhello. A and dynamic library file libmyhello. So.

# Gcc-C hello. c

# Ar Cr libmyhello. A hello. o

# Gcc-shared-fpci-O libmyhello. So hello. o

# Ls

Hello. c hello. h hello. O libmyhello. A libmyhello. So main. c

#

Through the last LS command, we can find that the static library file libmyhello. A and the dynamic library file libmyhello. So have been generated and are all in the current directory. Then, run the GCC command to use the function library myhello to generate the target file hello and run the program Hello.

# Gcc-O hello main. C-L.-lmyhello

#./Hello

./Hello: Error while loading shared libraries: libmyhello. So: cannot open shared object file: no such file or directory

#

It is easy to know from the results of program Hello running that when the static library and the dynamic library have the same name, the GCC command will give priority to the dynamic library.

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.