The source code computer in the program is not recognized, it is necessary to turn the written code into 0, 12 code, the computer can be recognized. The need to turn the source code into binary code takes two steps, compile, and link. Compilation is through the compiler to the code of each file into binary code, in this process, if there is a syntax error, there will be a compile failure prompt, if successful, then the corresponding multiple target files will be generated. Other files may be used in one file, so it is also necessary to combine the build-generated target file with the system-provided files, which is a link. After the link, the executable file is finally generated.
It is generally understood that the program is compiled and linked in two stages, but in fact, before compiling the preprocessor to pre-processing operations, and then go into the compilation phase. Because pre-processing instructions go ahead before compiling, they are more efficient than when the program is running.
The preprocessor is actually a statement that is processed before the parser, and it can identify specific statements that walk in the program. All preprocessing statements begin with a pound sign (#), which must be the first non-null character in a row.
Preprocessing statements can be roughly divided into three categories: file inclusions, macro definitions, and conditional compilation, which are explained in hit.
file contains
File inclusion refers to the use of functions or methods in other files in the current file, or other information, you can include the header files of other files, and then used in the current file, the file contains generally placed at the beginning of the file.
If you are programming in C, the file contains #include<> or #include "". If you use the Objective-c language, the file is included as #import<> or #import "". The biggest difference between the #include与 #import is that #import is checked when the file is imported. In addition, "" and <> two are also different, "" is used to put their own written files,<> used to put system files. In the execution of the program, according to the style you write, priority to find the corresponding type of file. such as <>, will go to find the system files, if not found, then to find the custom files. Therefore, the correct choice of style, can improve the efficiency of the program operation.
In the use of file inclusion, you will encounter a file used in the B file, b file used in a file, the mutual use of the relationship, which is a bit like a dead loop, to use a file, must first have B, but in B, but also need a, so in the run, there will be errors. The best way to solve this problem is to use @class instead of file inclusion, @class is to indicate that there is this class, and so on when the source file is really used to include the file.
Code:
//A.h//Test////Created by Jerei on 15-7-24.//Copyright (c) 2015 jerehedu. All rights reserved.//#import<Foundation/Foundation.h>@classB;@interfaceA:nsobject@property (nonatomic, strong) B*obj;@end////B.h//Test////Created by Jerei on 15-7-24.//Copyright (c) 2015 jerehedu. All rights reserved.//#import<Foundation/Foundation.h>@classA;@interfaceB:nsobject@property (nonatomic, strong) A*obj;@end
Macro definition
In the program, there are some constants or short functions that will be reused many times, for these commonly used data, we can use the macro definition. Using a macro definition allows you to quickly complete multiple configurations in a program, and the biggest benefit is that all values defined by the macro are changed whenever you modify the values defined by the macro. In addition, macro definitions are replaced and set before the program is compiled, and are more efficient than defining a global variable or function.
Macro definitions are implemented by # define, which is typically written under the file contained in the program. Macro names are usually expressed in all uppercase letters. Here's a look at the use of the macro definition in code.
Code:
//main.m//Test////Created by Jerei on 15-7-24.//Copyright (c) 2015 jerehedu. All rights reserved.//#import<Foundation/Foundation.h>#defineJR_PI 3.14#defineJr_max (A, B) ((A>B)? ( A):(B))//get a larger value in two numbers#defineJr_square_1 (n) n*n//calculate the square of a number#defineJr_square_2 (n) (n) * (n)//calculate the square of a number#defineJr_hello @ "HELLO World";intMainintargcConst Char*argv[]) {@autoreleasepool {intNUM1 = Jr_max (1,2); NSLog (@"max =%i", NUM1); //Result: max = 2 intnum2 = Jr_square_1 (2); NSLog (@"Square of 2 =%i", num2); //Result: 2 squared = 4 intnum3 = Jr_square_1 (2+1); NSLog (@"(2+1) squared =%i", num3); //Result: (2+1) squared = 5 intNUM4 = jr_square_2 (2+1); NSLog (@"(2+1) squared =%i", NUM4); //Result: (2+1) squared = 9 } return 0;}
As you can see in the code, the same is the square of a number, but the result of two macro definitions is different. The first calculation 2+1 the square of the time is 2+1*2+1, so the result is 5, the answer is wrong. Therefore, when you write a macro definition, with parameters, you need to set the parentheses to ensure correctness.
Conditional compilation
Conditional compilation is actually before compiling by the preprocessor to judge according to the preprocessing statement, if the condition is satisfied, the code snippet below the condition is satisfied, if the condition is not satisfied, the following code snippet does not enter the compilation link.
Conditional compilation is divided into two main types, one is to determine whether a macro has been defined, depending on whether the macro is defined, to determine whether to compile a piece of code. In addition, there is a set of statements and conditional structure in the ladder if structure is very similar, but the wording is different, is # if, #elif, #else, #endif组成. It is important to note that either way, there is a #endif end sign. In addition, the most important point is that conditional compilation conditions cannot use normal variables, and generally choose to use macro definitions.
Code:
//main.m//Test////Created by Jerei on 15-7-24.//Copyright (c) 2015 jerehedu. All rights reserved.//#import<Foundation/Foundation.h>#defineJr_count 10intMainintargcConst Char*argv[]) {@autoreleasepool {#ifDefined (Jr_count)NSLog (@"defines the macro for COUNT");#endif #ifDefined (Jr_max)NSLog (@"There is no definition of JR_MAX this macro");#endif #ifJr_count==1NSLog (@"jr_count=1");#elifjr_count==2NSLog (@"jr_count=2");#elifJr_count==3NSLog (@"jr_count=3");#else NSLog (@"jr_count=%i", jr_count);#endif } return 0;}
Inquiries or technical exchanges, please join the official QQ Group: (452379712)
Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
IOS preprocessing statements