Links, mounts, and libraries for C programs under Linux (3)

Source: Internet
Author: User
Tags ming naming convention

9. Target files are put together--static libraries.

Your co-workers are giving too many goals, from ONE.O two.o ... to your colleagues. Until the XXX.O.

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

First, look at your own source files to see which functions are used;

Second, it is very easy to identify which target file these functions come from, in this case, because the name of the function is corresponding to the name of the target file, such as ONE.O, which provides a function called one;

Third, use LD to link your own target files and the target files found in step two to generate the executable file.

The first step is a time-consuming process;

The second step is also not simple, because, you can not assume that the name of the function and the name of the target file is corresponding, this article just to cite an example;

The third step, it is also very troublesome, because you want to manually enter the name of these target files, in case of typos what to do?

So, your co-workers will be ONE.O two.o .... Until XXX.O unified into a file, called GOODTHINGS.A, this GOODTHINGS.A contains all the target files.

You only need to do this when you use it, assuming your own target file is called MAIN.O,

LD MAIN.O Goodthings.a-o Go

Simple and clear! One Step success! The LD command automatically searches the GOODTHINGS.A for the target files used by MAIN.O and links them.

Well, formally introduce GOODTHINGS.A this new "character".

This thing is a static library. (On the word static, back again)

To put it bluntly, the target file is packaged and placed in a piece.

The AR command can be used to make a static library:

Ar-r goodthings.a one.o two.o three.o ..... xxx.o.

This will be able to put ONE.O until XXX.O in GOODTHINGS.A.

You can see which target files are in a static library file:

Ar-t GOODTHINGS.A

However, it is recommended that the name of the static library be called a format such as LIBXXX.A, which is more in line with the naming convention, which is, in effect, a good naming convention. Because it can be used like this:

LD Main.o-l. -lxxx-o Go

LD will look for libxxx.a in this directory. Of course, here's the assumption that you put libxxx.a in this directory. Where-L. That is, in this directory to find the meaning.

10. Static to dynamic.

In front of the concept of loading, it is to move the program from the hard disk to the memory to begin execution.

Can simply say:

Static: The link process has been completed before loading.

Dynamic: Wait until the loading time to find the corresponding library file, and then link.

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

Dynamic, not so, when executing, go to find what I need, then link, then load.

Discuss the benefits of dynamic.

For example, watch TV.

Xiao Ming and Xiao Qiang want to watch TV, so they are carrying a TV, it seems, a little tired.

Xiao Ming said, put the TV in the room, ah, so, they in a room, open two TV, are looking at a station, it seems, a little wasted.

Xiao Ming said, since looking at a Taiwan, that close one, it seems, this is very good.

If Xiao Ming and Xiao Qiang at the same time use the ONE.O, then can launch a result, Xiao Ming and Xiao Qiang's executable file, must contain this ONE.O.

First, a waste of hard disk space; (two executable files contain ONE.O)

Second, when the program loads, the memory is wasted. (Since the ONE.O was loaded two times)

Dynamic library, you can solve the above two problems.

The same code, the hard disk should be able to exist only one copy, in memory should only exist in order to achieve the savings.

This is why, dynamic linking is the time when the program is loaded, only to link. (Because the early link, do not occupy the hard disk space?)

That's why, dynamic linking can save memory. (Because the same library, I only link once, put in memory, the second executable file can still share the library)

Links, mounts, and libraries for C programs under Linux (3)

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.