Differences between. h and. cpp files

Source: Internet
Author: User

Differences between header files and source files

First, we can put everything in a. cpp file.

Then the compiler will compile this. cpp into. obj. What is obj?

It is a compilation unit. A program can be composed of a compilation unit,

It can also contain multiple compilation units. If you don't want to make your source code hard to read,

Please use multiple compilation units. (A function cannot be placed in two compilation units, but more than two

You can put them in one unit, that is, cpp)

A. cpp corresponds to a. obj object, and all the obj objects are linked (through a program called the linker ),

It is a program.

What if one. cpp uses a function defined by another. cpp? Write his function declaration in this. cpp type.

The other work is done by the linker. You can call this function at will.

The linker connects all obj objects, but what if the same function or external variable happens? How does he identify it?

Generally, two identical function names or external variable names cannot appear in the same program.

Fortunately, c ++ can be limited by a keyword called the link property. Your function belongs to the whole program.

It is only used in a compilation unit obj.

These keywords are extern and static. extern indicates the external link, that is, in addition to this unit, the external unit.

Static is an internal link and belongs to its own unit.

After talking about it for so long, I haven't talked about the role of. h yet?

In fact, it can work well without. h, but when you find a function or external variable with an external link, you need many copies.

Declaration, because c ++ is a language that must be declared when using functions and variables. Why? Only after declaration

Only by knowing its specifications can you better find out what is not the same as the specifications.

The compilation unit obtains information about how you define the function.

Therefore, as long as the unit of the function is used, a declaration must be written in that. cpp. Is this very troublesome,

If you want to modify it, you must modify it one by one. This is really unbearable.

. H is born to solve this problem. It contains these public things. Then all. cpp that needs to use this function only

You can use # include to include it. You need to modify it later, but you only need to modify the content.

Please be careful not to abuse. h, do not write code in. h. h is not a. cpp repository, and everything is put in it.

If you write code in it, when other. cpp contains it, the definition will be repeated,

For example, put the function func () {printf}; in the header file a. h, there are some declarations required by a. cpp;

Then you find that B. cpp needs to use a function in a. cpp, so you are very happy to include a. h.

Note: # include is not an application instruction. It means to copy the content of the specified file intact.

Come in.

In this case, both a. cpp and B. cpp have the definition of a func () function.

If the function is static with internal links, it would be a waste of space;

If it is extern, external links (this is the default situation), it cannot appear in the same program according

If the same name function is required, the connector will show you a connection error without mercy!

------ Turning from Linux C/C ++ is my belief

---- Diligent Li Wenyu

 

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.