Links, loads, and libraries of c Programs in linux (3), linux

Source: Internet
Author: User

Links, loads, and libraries of c Programs in linux (3), linux
9. Put the target file together --> static library.

Your colleague has given too many target files, from one. o two. o ...... ...... Until xxx. o.

Well, if you really want to use the existing target files provided by your colleagues, you have to do three things:

1. Check your source files to see which functions are used;

2. Identify the target file from which these functions come from. In this example, It is easy because the function name corresponds to the target file name, such as one. o provides a function named one;

3. Use ld to link your target files with those found in step 2 to generate executable files.

The first step is a very time-consuming process;

The second step is actually not simple, because you cannot assume that the function name corresponds to the name of the target file. This article is just an example;

Step 3 is also very troublesome, because you need to manually enter the names of these target files. What should I do if I make a mistake?

So, your colleague ...... ...... Until xxx. o is unified into a file named goodThings. a. goodThings. a contains all the target files.

You only need to do this when using it. Assume that your target file is called main. o,

Ld main. o goodThings. a-o go

Concise and clear! One step succeeded! The ld command will automatically search for the target file used by main. o in goodThings. a and link it up.

Well, we will officially introduce goodThings. a, the new "character ".

This is a static library. (I will talk about the word static later)

To put it bluntly, it is to package the target file and put it in one piece.

The ar command can be used to create a static library:

Ar-r goodThings. a one. o two. o three. o ............ Xxx. o

In this way, we can put one. o till xxx. o in goodThings..

You can view the target files in a static library file:

Ar-t goodThings.

However, we recommend that you name the static library in the format of libxxx. a to better comply with the naming rules. In fact, such naming rules are advantageous. Because it can be used as follows:

Ld main. o-L.-lxxx-o go

Ld will find libxxx. a in the local directory. Of course, here we assume that you have put libxxx. a in the local directory. -L. indicates the meaning of searching in the local directory.

10. Static to dynamic.

The concept of loading is to move the program from the hard disk to the memory to start execution.

To put it simply:

Static: the link process is completed before loading.

Dynamic: when loading, find the corresponding library file and link it.

About static, once and for all, everything is here, and when you want to execute it.

Dynamic. This is not the case. During execution, find what I need, link it, and load it.

Discuss the dynamic benefits.

For example, watch TV.

Xiao Ming and Xiao Qiang both watch TV, so they seem tired when carrying a TV.

James said, put the TV in the room. Well, they are both watching one TV in one room. It seems a waste of time.

James said that it seems very good to close one platform.

If Xiao Ming and Xiao Qiang use one. o at the same time, a result can be released. The executable files of Xiao Ming and Xiao Qiang must contain one. o.

First, hard disk space is wasted. (Two executable files contain one. o)

Second, memory is wasted during program loading. (Because one. o is loaded twice)

The dynamic library can solve the above two problems.

In the same code, only one copy of the hard disk can exist, and only one copy of the memory should exist to save the performance.

This is why dynamic links are linked only when the program is loaded. (Because the link is early, will it occupy the hard disk space)

This is why dynamic connections can save memory. (Because of the same library, I only link it once and put it in the memory. The second executable file can still share this 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.