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

Source: Internet
Author: User
Tags ming

5. Redefine the error.

In a final executable, it is absolutely not allowed to have two global variables with the same name, and global functions with the same name are not allowed.

Global functions: All are global, as long as the functions decorated with the static modifier are not used.

Global variable: A variable that is declared outside the function and is not decorated with the static modifier.

For example, there is a function in one.c, so if you want the MAIN.O generated by MAIN.C to be able to link one.o, then there can be no more function called functions in MAIN.C. Otherwise, a redefinition error is reported.

It's like, there are two people in your class called Xiao Ming, this situation is really troublesome, you will certainly use various methods to distinguish them, for example, size Ming, Xiao Ming, this method, in fact, you have renamed them.

6. Declarations and definitions.

You may have found a problem, that is: there is a sentence in main.c

extern void function ();

This sentence is not the same name as the function in one.c. So why does the compiler not error?

Think about the whole process.

The first step:

Gcc-c Main.c-o MAIN.O

This step is to compile the main.c process, OK, this step, the compiler does not care about a place and a one.c, the source file has a function of the same name.

Explain this sentence carefully:

extern void function ();

When you write a program, you have a feeling of communicating with the compiler at all times.

This is what you write to the compiler, and you're going to tell the compiler the truth:

--hey! The compiler!

--MAIN.C This source file to use a function functions, is void type, no parameters, you do not have to control the function in the end, you first compile through, this function will eventually be your buddy link chain to take over!

Compiler:

--ok! I believe my buddy linker!

This process is called "declaration". Without this sentence, without this process, the compiler will encounter a strange function called functions in the MAIN.C, well, the result is, defeat!

and the real definition of this function is in one.c. The definition is the concrete implementation, which is what this function braces. It is also possible to say that there is no curly brace (even if there is no extern modifier) where the declaration, with curly braces (even if the curly braces are empty), is the definition.

There is also a statement that can be repeated many times, for example, every place where function functions are used, declare (you can try it without the extern modifier). However, there is only one definition, which is also in line with the previous section: cannot be redefined!

7. From hard disk to memory--load.

This section is not prepared to summarize too thin.

The source files you write are placed on the hard disk, and the target files you compile are also placed on the hard disk, and the linked executables are placed on the hard disk.

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

It can be simply outrageous to assume that the operating system copies your executable directly to somewhere in memory, and then the CPU starts to look for the main function in this place and then executes the entire program.

So your program will take up the memory space: the variables will take up, the function will occupy, the dynamic opening up (for example, malloc) will occupy more.

8. Save a bit of memory.

Your colleague is very competent, he used his leisure time, actively expand the one.c he maintained, make this one.c richer. For example, he added a function

int add (int a, int b)

{

Omit code

}

But, you know, the function eventually takes up memory space. And, you simply don't have the added function Add.

The problem is that when you link, the entire ONE.O is linked to your executable file. The executable actually contains the code for the implementation of the add, which means that in the final run, it does have this part in memory and it's completely useless.

You just used a function in the ONE.O to link the whole one.o, it's like, you go to a restaurant, just want to eat a hamburger, but have to pay to buy a package, waste.

You have to explain the problem to your colleague, and finally you have a plan.

Your colleague decided to put each function he wrote in a separate source file, for example, the function is put to FUNCTION.C, and the Add function is put to ADD.C.

Thus, a function corresponds to a source file, and also to a target file, that is, there is only one function in a target file, there is no other thing.

When you use it, you can pick and choose which target file to link to.

For example, your colleague already has these:

ONE.C contains the void One () {} function

TWO.C contains the Void () {} function

THREE.C contains the void three () {} function

Wait a minute....... And it also provides a header file called All.h, which contains a declaration of all the functions he wrote.

When you use it, include the all.h, like this.

#include "all.h"

Then, compile, and then link the time you use, just add that target file to your GCC command. For example, the void three () function is used:

LD MAIN.O Three.o-o Go

That's all.

But even if you still feel the trouble, you have to keep a record of what functions you have used, and to manually tap the command to link it, which makes it error prone.

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

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

Related Article

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.