A probe into the development of a shared library (dynamic link library) of GCC under Linux

Source: Internet
Author: User
Tags tmp file

Here is a list of some of the simple questions I encountered while writing and using the shared library (dynamic link library) and the answer:

Reference Document: Http://www.cnblogs.com/likwo/archive/2012/05/09/2492225.html

1. What is the difference between a static library, a dynamic link library, and a shared library?

The Static library (under Windows Lib,linux. A) is compiled into the target program before the program is written, and the dynamic-link library (. dll under Windows) can be dynamically loaded at any time the program executes. The shared library (. So under Linux) is loaded into the program when the program starts.

1). What is the difference between a dynamic-link library and a shared library?

I personally think they can be seen as being the same. If you really want to understand the differences in their composition, you can refer to this article: http://www.cnblogs.com/likwo/archive/2012/05/09/2492225.html

2. Why use the. so file?

1. Because the shared library can be dynamically loaded into memory by the application. Therefore, the application can load. So into memory when needed, which makes the program more maintainable. For example, QQ video function needs to be upgraded, then the programmer responsible for writing QQ does not have to rewrite all the QQ code, just the video features related to the. so file rewrite.

2. The generated. So file can be thought of as a function, and other programs can call these functions directly in their code to do the same thing (this should be called a closed source, as opposed to other functions that directly give the source code). Such a library (code) can be repeated calls, thereby reducing the amount of code written to achieve the modularity of the code.

3. How can the. so/.dll file be generated?

1. For the generation of. dll files, refer to my brief introduction: http://www.cnblogs.com/vincentX/p/4798830.html

2. Generate the. So file:

To give a simple example, let's implement the two-number addition function:

1. First create a new 2 files, named Test.h test.c tmp.c

2. Write the following code into the Test.h file:

1 int Add (intint);

3. Write the following code into the test.c file:

1 " test.h " 2 3 int Add (intint  b) {4     return a + b; 5 }

4. Compile the test.c into a. So file, and bash commands are as follows:

1 gcc test.c-shared-fpic-o test.so

Shared is to generate shared libraries,-fpic is declared using location-independent code. I'll answer it later on why I need to join-fpic.

In this case, a. So file is generated in the current directory.

5. Next write the following code into the Tmp.c file:

1#include <stdio.h>2#include"test.h"3 4 intMain () {5printf"Hello world\n");6printf"%d\n", Add (2,3));7     return 0;8}

6. Compile tmp.c:

1 gcc tmp.c-o tmp./test.so

At this point, we can./tmp to test the normal use of the just-written. so file. Execute the TMP file:

1 ./tmp

The results are as follows:

1 Hello World 2 5

Indicates that the generated shared library can be used.

Let's do a little experiment: what happens when you delete the. So file that you just generated?

1 RM test.so 2 ./tmp

This will return an error message:

1  while Object file file or directory

The shared library test.so contained in TMP could not be found. It can also be explained that the executable file is indeed a. So file that was called before it was run (because the Hello World is not output at this time). When we re-compile the test.so and then execute the TMP, it will still work.

4. About-fpic?

The standard practice for making a shared library under Linux is to compile the. so file into location-independent code, and then build the. so file. So I'm thinking: is-fpic necessary? It is also possible to run normally without adding-fpic.

A detailed explanation of the-fpic can be found in: http://www.cnblogs.com/cswuyg/p/3830703.html

 

Reprint please declare the source, thank you for your cooperation.

A probe into the development of a shared library (dynamic link library) of GCC under Linux

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.