Relationship between header files and source files

Source: Internet
Author: User
Tags constant definition
I. How to associate the source file with header file 1 according to # include? 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 <XXX. h>
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"
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
1. It declares a function without a detailed description of the function body.
2. It contains an extern definition and does not initialize a function or function body.
3. It is a declaration of a static class data member included in a class definition. It must be exactly defined at a certain place in the final program.
4. It is a class name declaration, such as class test;
5. 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, such as int A = 2;
3. The declaration of non-static data members is also the definition ???
4. The declaration of all member functions of the class is to define a definition as 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. The Declaration is only useful to the current compilation unit and will not affect it. 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. To sum up, the. h file can include:
 
1. Class Member Data declaration, but cannot assign values 2. Class static data member definition and assignment, but not recommended, just a declaration.
3. Class member function declaration
4. Non-class member function Declaration 5. constant definition: for example, const int A = 5; 6. static Function Definition 7. class inline function definition cannot include: 1. declaration 2 of all non-static variables (not a data member of the class. 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.