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

Source: Internet
Author: User

Links, loads, and libraries of c Programs in linux (2), linux
5. Redefinition error.

In a final executable file, two global variables with the same name are definitely not allowed, or global functions with the same name are not allowed.

Global functions: All functions without static modifier are global.

Global variables: variables declared and defined outside the function without the static modifier.

For example, one. c has a function, so if you want to make main. main. o can be linked to one. o, then main. c cannot have another function called function. Otherwise, a redefinition error is reported.

It seems that there are two people in your class called James, which is really troublesome. You will certainly use various methods to distinguish them, such as James and James. In fact, you have renamed them.

6. Declaration and definition.

You may have found a problem:

Extern void function ();

Isn't this the same as the function in one. c. So why does the compiler not report an error?

Think about the entire process.

Step 1:

Gcc-c main. c-o main. o

This step is the process of compiling main. c. Well, in this step, the compiler does not need to worry about another one. c In some places. This source file contains a function with the same name.

Explain this sentence carefully:

Extern void function ();

When you write a program, you must have a sense of constant interaction with the compiler.

This is what you wrote to the compiler. You just want to tell the compiler the fact that:

-- Hey! Compiler!

-- Main. A function is used in the c source file. It is of the void type and has no parameters. You don't need to worry about where the function is. You compile it first, this function will eventually be linked by your buddy linker!

Compiler:

-- OK! I believe in my buddy chainer!

This process is called "Declaration ". Without this sentence, the compiler will encounter a strange function calling function in main. c!

The real definition of this function is in one. c. The so-called definition is the specific implementation, which is the things in the braces of this function. It can also be said that the place without braces (even if there is no extern modifier) is Declaration, where braces (even if they are empty) are defined.

Another sentence is that declaration can be performed multiple times. For example, declaration is required for every function used (you can try it without the modifier "extern ). However, there is only one definition, which also complies with the previous section: it cannot be redefined!

7. From hard disk to memory -- load.

This section is not too detailed.

The source files you wrote are stored on the hard disk, and the target files you compiled are also stored on the hard disk. the executable files linked to the files are also stored on the hard disk.

When you run this executable file, the operating system will do one thing: load.

You can simply and roughly imagine that the operating system copies your executable files directly to a place in the memory. Then, the cpu starts to find the main function in this place and then runs the entire program.

So your program occupies the memory space: variables, functions, and dynamic development (such as malloc.

8. Save a little memory.

Your colleague is very competent. He has spent his spare time actively expanding one. c He maintains to enrich one. c. For example, he adds a function

Int add (int a, int B)

{

// Omitting the code

}

However, you know, the function will eventually occupy the memory space. In addition, you cannot use the newly added function add.

The problem is that you have linked the entire one. o to your executable file. This executable file indeed contains the specific implementation code of add. That is to say, this part of memory does exist during the final running and is useless.

You only use one. A function in o is linked to the entire one. o, it's like you just want to eat a hamburger at a restaurant, but you have to spend money to buy a package, which is a waste.

You have to explain the problem to your colleagues and finally come up with a solution.

Your colleague decided to put each function he wrote into a single source file. For example, the function is put into function. c, and the add function is put into add. c.

In this way, a function corresponds to a source file and a target file. That is to say, there is only one function in a target file, and there is nothing else.

You can select the target file to be linked.

For example, your colleagues already have these:

One. c contains void one () {} Function

Two. c contains void two () {} Function

Three. c contains the void three () {} Function

And so on ....... In addition, a header file named all. h is provided. This all. h contains the declaration of all functions written by him.

You must include all. h, as shown in the following figure.

# Include "all. h"

Then, compile and add the target file to your gcc command when you use the link. For example, the void three () function is used:

Ld main. o three. o-o go

That's all.

Even so, you are still in trouble. You have to record in your mind what functions you have used and manually click the commands to link them. This is prone to errors.

Any problem can be solved, and this time is no exception.

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.