Linux dynamic library and static library

Source: Internet
Author: User
Function libraries can be divided into static libraries and dynamic libraries. Create a Linux static library and a Linux dynamic library and use them here. 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. Dynamic

Function libraries can be divided into static libraries and dynamic libraries. Create a Linux static library and a Linux dynamic library and use them here. 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.

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.

  1. Program 1: hello. h
  2. # Ifndef HELLO_H
  3. # Define HELLO_H
  4. Void hello (const char * name );
  5. # Endif // HELLO_H

Program 2: hello. c

  1. # Include
  2. Void hello (const char * name)
  3. {
  4. Printf ("Hello % s! N ", name );
  5. }

Program 3: main. c

  1. # Include "hello. h"
  2. Int main ()
  3. {
  4. Hello ("everyone ");
  5. Return 0;
  6. }

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

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

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.

Check the results.

  1. # Gcc-o hello main. c-L.-lmyhello
  2. #./Hello
  3. Hello everyone!
  4. #

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

  1. # Rm libmyhello.
  2. Rm: remove regular file 'libmyhello. '? Y
  3. #./Hello
  4. Hello everyone!
  5. #

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

Step 2: Use a dynamic library in a 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.

  1. # Gcc-o hello main. c-L.-lmyhello
  2. #./Hello
  3. ./Hello: error while loading shared libraries: libmyhello. so: cannot open shared object file: No such file or directory
  4. #

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.

  1. # Mv libmyhello. so/usr/lib
  2. #./Hello
  3. Hello everyone!
  4. #

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.

  1. # Rm-f hello. o/usr/lib/libmyhello. so
  2. # Ls
  3. Hello. c hello. h main. c
  4. #

Create the static library file libmyhello. a and dynamic library file libmyhello. so.

  1. # Gcc-c hello. c
  2. # Ar cr libmyhello. a hello. o
  3. # Gcc-shared-fPCI-o libmyhello. so hello. o
  4. # Ls
  5. Hello. c hello. h hello. o libmyhello. a libmyhello. so main. c
  6. #

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.

  1. # Gcc-o hello main. c-L.-lmyhello
  2. #./Hello
  3. ./Hello: error while loading shared libraries: libmyhello. so: cannot open shared object file: No such file or directory
  4. #

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

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.