Here I would like to talk about my understanding of the above (such as improper, but also ask your classmates to correct):
1. To enhance readability and avoid implicit dependencies, use the following sequence: C standard library, C + + standard library, header files for other libraries, and your own project header files. However, this first contains the preferred header file, i.e. the A.cpp file should have a precedence of a.h. The preferred header file is to reduce the hidden dependency while ensuring that the header file and the implementation file are matched. The specific example is: If you have a CC file (the Linux platform cpp file suffix cc) is google-awesome-project/src/foo/internal/fooserver.cc, then it contains the order of the header files are as follows:
#include <sys/types.h> #include <unistd.h> #include
2. When you include the header file, you should add the folder name of the project where the header file is located, that is, if you have a project base with a logging.h, then the external include this header file should be written like this:
#include "Base/logging.h" instead of # include "Logging.h"
What we see here is the hidden purpose behind the principles advocated by the Google C + + programming style guide:
1. In order to reduce the hidden dependency, the header file and its implementation file match, should first contain its preferences (that is, its corresponding header file).
2. In addition to the preferences, follow the general to the special principle. But I think the order of the Google C + + programming style guide: C standard library, C + + standard library, other library header files, your own project header file missing the first item: OS-level header files, such as the above example sys/ Types.h estimates are not included in the C standard library, but the SDK provided by the Linux operating system. So I think the more accurate statement should be: OS SDK. h, C standard library, standard C + + library, header files for other libraries, and your own project header files.
3. To list the project directory where the header file is located, the role should be the same as the namespace, in order to distinguish between the file names accidentally caused.
Two Different viewpoints in "C + + programming thought"
Unlike the Google C + + programming style guide, the C + + programming idea advocates a different rule. "C + + programming thought" P432 mentioned:
Header files are included in the order from "most special to most general". This is where any header files in the local directory are first included. Then there's our own "Tools" header file, followed by a third-party library header file, followed by the standard C + + library header file and the C library header file.
To understand the reason: you can see Johnlakos in the "Large scalec++ softwre Design" (Note: its Chinese translation is "large-scale C + + program Design") in a paragraph:
Ensure that the components of the. h file are not parsed by itself (parse), which avoids potential usage errors. Lack of explicitly provided declarations or definitions because of their own parsing. The first line of the. c file contains the. h file to ensure that all internal information blocks that are important to the physical interface of the component are in. h (this problem can be found once the. c file is compiled if some block of information is indeed missing).
If the order that contains the header file is "from most special to most general", if our header file is not parsed by it itself. We will find it immediately to prevent trouble from happening.
Three My experiment.
What kind of containment sequence is good? I use VS 2005 to make a console test project Testinc, which has several files.
The code for MYMATH.H is as follows:
#pragma once double ACOs (double Num); The code for MyMath.cpp is as follows: Double ACOs (double Num) { return 1.0; }
The code for TestInc.cpp is as follows:
#include "TestInc.h" #include <stdio.h> #include <math.h> int _tmain (int argc, _tchar* argv[] ) { Double A = ACOs (0.5); return 0; }
Error occurred:
1>c:program FilesMicrosoft VisualStudio 8VCINCLUDEMATH.H (107): Error C2732: Link specification conflicts with "ACOs" early specification 1> c: Program FilesMicrosoft Visual Studio 8VCINCLUDEMATH.H (107): See Declaration of "ACOs"
I then changed the TestInc.cpp header file to include the following sequence:
#include <stdio.h> #include <math.h> #include "TestInc.h"
The compilation is passed. In the debug run, the main function call or the C standard library function ACOs, it appears that the order of the function call is in the order of the header file, that is, my custom ACOs function is overwritten (if the TestInc.h contains inline functions, the first call is an inline function).
From this little experiment, I came to the conclusion that the Google C + + programming style guide and C + + programming ideas advocated by the order of including header files have advantages, "Google C + + programming style guide" should be able to reduce the hidden header file dependencies, and "C + + programming Ideas" It is easy to let you know if the interface you are defining conflicts with the system library and the third-party libraries.
Four The precompiled feature in the header file contains
Development in Visual Studio environment we find that almost every CPP file contains the StdAfx.h file, and it's going to be a mistake to put it in the first place. What is this for?
Originally, Visual Studio adopted a pre-compiled mechanism. To understand the precompilation mechanism, first introduce the precompiled header. A precompiled header is a piece of code in a project that is pre-compiled and placed in a file (usually with a. pch extension), which is known as a precompiled header file. These precompiled code can be any C + + code, even an inline function, but must be stable, In the process of engineering development will not be changed frequently. If the code is modified, you will need to recompile the build precompiled header file. Note It is time consuming to generate precompiled header files. Also note that precompiled header files are usually large, usually 6-7m. Be careful to clean up the unused precompiled header files in a timely manner.
You may ask: Now the compiler has the function of time stamp, when the compiler compiles the entire project, it compiles only those files that have been modified, not those that have not been modified since the last compilation. So why precompile the header file? The answer here, we know that the compiler is compiled in a file, a file has been modified, will recompile the entire file, of course, in this file contains all the header files (. eg Macro, preprocessor) to be processed again. VC's precompiled header file is exactly this part of the information saved. To avoid having to re-process the header files every time.
According to the above, the role of precompiled header is of course to improve the speed of the cheap, with it you do not have to compile every time that do not need to change the code frequently. The compilation performance is of course improved.
To use a precompiled header, we have to specify a header file that contains the code and other header files that we will not change frequently, and then we use this header file to generate a precompiled header file (. pch file) and presumably everyone knows StdAfx.h this file. Many people think that this is a "system level" provided by the VC, the compiler with a header file. Not really, this file can be any name. Let's examine a typical precompiled header file for the MFC Dialog Based program generated by AppWizard. (Because AppWizard will give us a good way to use the precompiled header file, the default is StdAfx.h, which is the name of VC). We will find this header file contains the following header files:
#include <afxext.h>//MFC Extensions #include <afxdisp.h>//MFC Automation Classes #include < Afxdtctl.h>//MFC support for Internet Explorer 4 Common Controls #include <afxcmn.h>
These are the headers that must be included with MFC, and of course we are not likely to modify these header files in our project, so that they are stable.
So how do we specify it to generate precompiled header files. We know that a header file is not compiled. So we also need a CPP file to generate the. pch file. The default for this file is StdAfx.cpp. In this file there is only one line of code: #include "Stdafx.h". The reason is that we just want it to compile--that is, it's just the. cpp extension. We can use the/yc compile switch to specify StdAfx.cpp to generate a. pch file, specifying the name of the generated PCH file by/fp the compile switch. Open the Project->setting->c/c++ dialog box. Point the category to the precompiled Header. Select the entire project in the tree view on the left, project options (the white place in the lower-right corner) to see the/fp "debug/pch.pch", which is the name of the. pch file that is specified, and the default is usually. pch. Then, in the tree view on the left, select StdAfx.cpp, and the original project option becomes the Source file option (originally a project, now a file, of course, changed). Here we can see the/yc switch, the role of/yc is to specify this file to create a PCH file. The file name behind/yc is the header file that contains the stable code, and only one file can have a YC switch in a project. VC compiles StdAfx.cpp into an obj file and a PCH file based on this option.
In this way, we set up the precompiled header file. In other words, we can use the precompiled header feature. Here are some things to note:
1) If/yu is used, that is, pre-compilation is used, we start at the very beginning of each. cpp file, including the. h file you specified to produce the PCH file (default is stdafx.h) or there will be a problem. If you do not include this file, you will be told unexpected file end.
2) If you accidentally lost the PCH file, according to the above analysis, you just have to let the compiler generate a PCH file. That is, the stdafx.cpp (that is, the CPP file of the specified/yc) will be recompiled again.
Is there such a pre-compilation mechanism under the Linux platform? If so, how is it implemented? The GCC compiler also implements the pre-compilation mechanism under the Linux platform. Here is an example of an open source IDE codeblocks (codeblocks built-in GCC compiler) to illustrate the implementation of the Linux platform:
Use Codeblocks to build a C + + project, then create a new MY_PCH.H and enter the following code:
/*************************************************************** * Name: my_pch.h * Purpose: Header to create pre-compiled header (PCH) * Author: () * Created: 2010-10-26 * Copyright: () c10/>* License: * How to use: Project build Options-and other options-fill in the following two lines -winvalid-pch -include my_pch.h ********* / #ifndef my_pch_h_included #define My_pch_h_ INCLUDED //Put here all your rarely-changing header files #include <iostream> #include <string > #endif
Then in the project build options –> Other options –> fill in the following two lines
-winvalid-pch-include MY_PCH.H
You can enable precompiled file headers.
Then main.cpp can be used without the include header file, directly so that you can compile the
int main () { using namespace std; cout << "Hello world!" << Endl; return 0; }
Even if the code above is written on the following line, it doesn't really work:
#include <iostream>
The above is the C + + header file contains the order of research content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!