Relationship between header files and source files based on C ++

Source: Internet
Author: User
Tags constant definition

 

Today, I found an open-source C ++ project tinyxml that parses xml and compiled it according to the instructions on the Internet, but the compilation failed. "The header file tinyxml cannot be opened. h ", but clearly I have this file under the project, for C ++ beginners like me, I don't know the relationship between header files and source files (unlike java class files), but I finally solved this problem.

1. How to associate the header file with # include in the source file

1. the header file that comes with the system is enclosed in angle brackets, so that the compiler will find it in the system file directory.

# Include

2. The custom file is enclosed in double quotation marks. The Compiler first searches for the file in the user directory, then, go to the C ++ installation directory (for example, you can specify and modify the library file search path in VC, And you can set environment variables in Unix and Linux) to find the path, finally, search for it in the system file.

# Include "xxx. h" (I always thought "" is no different from <>, but tinyxml. h is a non-system file, so it must be "")

Ii. How to associate header files with source files

In fact, the header file ". h "declares a series of functions," B. cpp ", if I want. use ". h. the functions implemented in cpp are usually in c. # include ". h ", then c. how does cpp find B. what about the implementation in cpp?

In fact, there is no direct relationship between. cpp and. h file names. Many compilers can accept other extensions. For example, we can see the source code of our company. The. cpp file is replaced by the. cc file.

In Turbo C, the command line is used for compilation. The command line parameter is the file name. The default values are. cpp and. h, but can also be customized to. xxx.

As mentioned in the C Program Design book by Mr. Tan haoqiang, the # include command should be "File Inclusion processing" during compiler preprocessing ": copy all contents of file2.c to # include "file2.c. This also explains why many compilers do not care about the file suffix-because # include preprocessing completes a "copy and insert code" job.

During compilation, the function implementation in the B. cpp file is not found, and this work is only performed during link. We are in B. cpp or c. # include ". h "is actually the introduction of relevant declarations, so that compilation can pass, the program does not care where the implementation is, how it is implemented. After the source file is compiled, the target file (. o or. obj) is generated. In the target file, these functions and variables are considered as symbols. During link, you need to specify the connection in makefile. o or. obj file (here is B. the. o or. obj file), the connector will go to this. o or. find the obj file in B. the functions implemented in cpp are then built into the executable file specified in makefile.

In Unix, you can even exclude the header file from the source file. You only need to specify the name in makefile (however, this greatly reduces the readability of the program, which is a bad habit ). In VC, you do not need to write makefile by yourself. You only need to include all the files in the project. VC will automatically help you write the makefile.

Generally, the compiler will find the desired symbol in each. o or. obj file, instead of finding or finding one in a file. Therefore, if the same function is implemented in several different files or the same global variable is defined, the system prompts "redefined" when linking ".

 

I. Distinction between declaration and definition:

A declaration is a definition, unless:

Declaration: Import Name

Definition: introduce an object

It declares a function without a detailed description of the function body.

It contains an extern definition and does not initialize a function or function body.

It is a declaration of static class data members included in a class definition. It must be exactly defined at a certain place in the final program

It is a class name declaration, such as class test;

It is a typedef declaration.

Implication:

1. Class declaration is Definition

2. At the same time, the Declaration of initial values is also the definition, as shown in figure

3. The declaration of non-static data members is also the definition ???

 

4. The declaration of all member functions of the class is also the definition.

A definition is a declaration, unless:

1. It defines a static data member

2. It defines a non-inline member function

Internal Connection and external connection:

During compilation, each file is compiled into a source file containing necessary information (also called a compilation unit), and then the compilation unit is linked into a file with the same name as the family file. o file ,. o files link the symbols generated in different compilation units to form an executable file. There are two completely different links: internal and external, linking these compilation units.

Internal Connection: access to this definition is limited to the current compilation unit, and other compilation units cannot be accessed.

External Connection: can be accessed by other units, so the name must be unique in the entire execution file.

Class Definition (also Declaration), enum, struct, are internal connections, inline functions, static non-class member data is also

The type declared by typedef is also inner join.

Non-inline member functions (including static members) have external connections, non-inline functions, and non-static free functions (non-class member functions) are also external connections.

Declarations are only useful to the current compilation unit, and they do not affect the. o file,

. H file, because the file will be other. cpp file inclusion, but since the Declaration is only valid for the current compilation unit, it will not introduce the symbol. o file, so this file cannot contain any External Connection Symbol (data member and function) Definition. Generally, do not include the definition of inner connection symbols.

 

In summary:

The. h file can contain:

Class Member Data declaration, but cannot be assigned a value

Class static data member definition and assignment, but not recommended, just a declaration.

Class member function declaration

Non-class member function declaration

Constant definition: for example, const int a = 5;

Static Function Definition

Class Inline Function Definition

Cannot contain:

1. Declaration of all non-static variables (not a class data member)

2. The default namespace Declaration should not be placed in the header file, using namespace std; and so on should be placed in. cpp, and std: string should be used in. h.

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.