Create static and dynamic libraries in Linux

Source: Internet
Author: User

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

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 output "Hello XXX!" on the screen! ".

Main. c is the main program for testing the library file, and the public function hello is called in the main program.

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. Below

Then run the hello program to check the result.

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

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 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

When the static library and the dynamic library have the same name, the GCC command will give priority to the dynamic library


Address: http://blog.csdn.net/jingjiwu/article/details/1995838

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.