Create a simple static and dynamic library

Source: Internet
Author: User
1. Vim test. H, test. C, and Main. C;

Test. H is the header file of the function library. Test. c is the source of the function library.ProgramContains the function test, which outputs printf ("Test for Lib. \ n") on the screen ");

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

2. compile test. c into the test. o file;

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

Gcc-O test. O-C test. c

3. Create a static library from the test. 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 we name the created static library mytest, the static library file name is libmytest.. 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 libmytest..

# Ar Cr libmytest. A test. o

 

4. Use static databases;

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 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 Main. C, we include the static library header file test. h, and then directly call the public function test in the main program. Below

Run the test program to check the result.

# Gcc-O test main. C-L.-lmytest

 

5. 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 we name the created dynamic library mytest, the dynamic library file name is libmytest. So. Use GCC to create a dynamic library.

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

# Gcc-shared-fpci-O libmytest. So test. o

 

6. Use a dynamic library;

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 test main. C-L.-lmytest

Test. H is the header file of the function library. Test. c is the source program of the function library, which includes the function test, which outputs printf ("Test for Lib. \ n") on the screen ");

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

2. compile test. c into the test. o file;

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

Gcc-O test. O-C test. c

3. Create a static library from the test. 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 we name the created static library mytest, the static library file name is libmytest.. 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 libmytest..

# Ar Cr libmytest. A test. o

 

4. Use static databases;

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 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 Main. C, we include the static library header file test. h, and then directly call the public function test in the main program. Below

Run the test program to check the result.

# Gcc-O test main. C-L.-lmytest

 

5. 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 we name the created dynamic library mytest, the dynamic library file name is libmytest. So. Use GCC to create a dynamic library.

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

# Gcc-shared-fpci-O libmytest. So test. o

 

6. Use a dynamic library;

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 test main. C-L.-lmytest

Test. H is the header file of the function library. Test. c is the source program of the function library, which includes the function test, which outputs printf ("Test for Lib. \ n") on the screen ");

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

2. compile test. c into the test. o file;

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

Gcc-O test. O-C test. c

3. Create a static library from the test. 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 we name the created static library mytest, the static library file name is libmytest.. 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 libmytest..

# Ar Cr libmytest. A test. o

 

4. Use static databases;

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 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 Main. C, we include the static library header file test. h, and then directly call the public function test in the main program. Below

Run the test program to check the result.

# Gcc-O test main. C-L.-lmytest

 

5. 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 we name the created dynamic library mytest, the dynamic library file name is libmytest. So. Use GCC to create a dynamic library.

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

# Gcc-shared-fpci-O libmytest. So test. o

 

6. Use a dynamic library;

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 test main. C-L.-lmytest

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.