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

Source: Internet
Author: User

After reading the "programmer self-cultivation-link, loading and library" related chapters, want to summarize, if there are errors, please correct me, thank you.

1. What is a target file?

Your project has a lot of xxx.c such as source files, these files are text files, only people can recognize (of course the compiler knows), but the CPU does not know. The problem is that the CPU is actually executing the instruction.

Let the compiler translate (there are many processes, this is not the focus of this article), in general, a xxx.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 the information about the source file, including the definition of functions in this source file, the definition of global variables, and so on.

But is it possible to execute this target file without worrying about it? No.

One, your target file may not have the main function;

Second, in your target file, other functions may be used, and these functions are defined in other target files. For example, MAIN.C used the Void function () in the one.c; You go to execute MAIN.C generated MAIN.O, certainly not ah, because the CPU can not find where function, thus function stored instructions, of course, can not execute;

In short, the file you want to run must have all the relevant information about functions and variables. It is clear that the target file does not have this feature. Because the destination file stores only its own information, it does not know the information of other target files.

2. Mosaic of target files---executable file

Well, you have a main.c and a one.c, and successfully generated two target files, each storing their own information, they are MAIN.O and ONE.O.

Unfortunately, MAIN.C used the Void function () in the one.c; Function. At this time, MAIN.O was unable to find out where the function was and could not be executed. And ONE.O quietly waiting there, waiting for a process.

This process is a link.

LD is an instruction that, under Linux, allows the target file to be linked together to make a real executable file.

For example, this:

LD MAIN.O One.o-o Go

Where-O is optionally specified, which is the name of the executable file. OK, this go is the final executable file. You can go and carry it out.

Go is made up of two target files, and it certainly knows all the information, including the specific function instructions. As a result, it can be executed.

Now, here we seem to forget another important file, the header file.

3. What is a header file?

Well, the question will improve your enjoyment of reading this article. Then think of a question: can MAIN.C be successfully compiled into MAIN.O?

Just the process seems to be too smooth, main.c brush to become a main.o, and the problem is that you used in main.c a function that it does not recognize void functions (); This could have been compiled, smoothly generated MAIN.O?

You can try it with this command:

Gcc-c Main.c-o MAIN.O

The-c option means that I want to generate the target file instead of the default executable file. You are bound to get a "compile" error, this error will tell you that function is not known to me, defeated!

"Compile" error, at the source of the first to prevent you from creating a completely useless program.

This time, how to do, main.c really do not recognize function functions, you can not put one.c in the function copy paste into the main.c it (of course, this is OK, but the low burst).

Then the requirements are as follows:

One, do not copy over the entire function;

Two, let main.c smoothly generate MAIN.O.

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

Easy, you add a sentence in the MAIN.C source file

extern void function (); The return type of this function is void and has no parameters.

In this way, MAIN.C itself knows the function, notice, just know, but do not know its implementation, do not know where this function. In fact, there is no need to know so much. Because, my step is just to generate the target file. The rest is handed over to the link that step.

conclusion, it is necessary to make the source file aware of each symbol (variables and functions) to generate the target file.

If ONE.C was written by your colleague, you should have him write a header file at the same time. You're going to have to add a line to your main.c,

extern void Function1 ();

extern int function2 (char a);

...

...

This stuff.

Your colleague will give you a header file One.h, the header file is actually the above extern content. You just have to do this in MAIN.C:

#include "one.h"

On the line, this is the whole copy of the one.h into the main.c.

4. printf is very cool to use.

printf used to be very cool.

You just write it.

#include <stdio.h>

Let your main.c know this function can be used.

The problem, though, is that you don't have a link to the target file where printf is located!

Think, think, think again!

Well, that's not a problem.

In fact, GCC is the default link for you. Do not think, no link can!!!

You can think: as long as the system provides things, you do not have to manually link, you care about your own things on the line.

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

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.