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

Source: Internet
Author: User

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

After reading the relevant chapter "Programmer self-cultivation-links, loading and libraries", I would like to summarize it. If there are any errors, please correct me. Thank you.

1. What is the target file?

There are many source files such as xxx. c In your project. These files are text files that only people can understand (of course the compiler knows), but cpu doesn't know. The problem is that the cpu actually executes commands.

Let the compiler translate (there are many processes here, which is not the focus of this article). Generally, a xxx. the c file can be translated into a xxx. o. This is the target file.

A source file corresponds to a target file, which stores all information about the source file, including the definition of functions and global variables in the source file.

However, can this object be executed without any worries? No.

1. You may not have the main function in this target file;

2. Other functions may be used in your target file, and these functions are defined in other target files. For example, main. c uses one. void function () in c; you can execute main. main. o, no, because the cpu cannot find where the function is, And the commands stored in the function cannot be executed;

In short, the file you want to run must contain information about all functions and variables. Obviously, the target file does not have this feature. Because the target file only stores its own information and does not know the information of other target files.

2. Target file splicing --> Executable File

Okay, you have a main. c and one. c, and two target files are successfully generated, respectively storing their own information. They are main. o and one. o.

Unfortunately, the void function (); function in one. c is used in main. c. At this time, main. o cannot find where the function is and cannot be executed. One. o quietly waited for a process.

This process is the link.

Ld is a command. in linux, you can link the target file to form an executable file that is actually usable.

For example:

Ld main. o one. o-o go

Here,-o is randomly specified, which is the name of the executable file. Well, this go is the final executable file. You can execute it.

Go is a combination of two target files. It certainly knows all the information, including the specific function commands. Therefore, it can be executed.

Now, we seem to have forgotten another important file, header file.

3. What is the header file?

Well, questions can improve your reading of this article. Let's think about one question: Can main. c be compiled into main. o?

The process just now seems so smooth, main. c Click it to become main. o, and the problem is that you are in main. c uses a function void function () that is not recognized by it. This function can be compiled successfully to generate main. o?

You can try the following command:

Gcc-c main. c-o main. o

-C option indicates that I want to generate the target file instead of the default executable file. You will surely get a [compilation] error, which will tell you that I don't know or lose the function!

[Compilation] error. It prevents you from creating a completely unusable program at the source.

What should I do at this time, main. c does not recognize function. copy and paste the function in c to main. c ).

The requirements are as follows:

1. Do not copy the entire function;

2. Let main. c generate main. o smoothly.

The core of the problem is to let main. c know what function is (a function or a variable? If it is a function, what are the parameters of this function? What type of value is returned ?).

Easy: add a sentence to the main. c source file.

Extern void function (); // The return type of this function is void, and there is no parameter.

In this way, main. c knows the function itself. Note that it only knows the function, but does not know its implementation or where it is. In fact, you don't need to know that much. This step only generates the target file. The rest is given to the link.

Conclusion: To generate a target file, the source file must recognize every symbol (variable and function ).

If one. c is written by your colleague, you should ask him to write a header file at the same time. Save, you need to add a line in your main. c

Extern void function1 ();

Extern int function2 (char );

...

...

Such a thing.

Your colleague will give you a header file one. h, which contains the above extern content. You only need to do this in main. c:

# Include "one. h"

That is, copy one. h to main. c.

4. printf is quite easy to use.

Printf is quite easy to use.

You only need to write

# Include <stdio. h>

Let your main. c understand this function.

However, the problem is that you have not linked the target file where printf is located!

Think, think, and think again!

Okay, this is not a problem.

Actually, gcc links you by default. Never think of it as a link !!!!

You can think like this: as long as it is something provided by the system, you don't need to manually link it. You just need to care about your own things.

 

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.