The following code directives are often seen in the code inside the Xcode IDE:
#pragma mark-
#pragma mark Initialization
What's #pragma?
Technically, the code that starts with #pragma is a compiler directive, which is a program-or compiler-specific instruction. They do not necessarily apply to other compilers or other environments. If the compiler does not recognize the directive, it is ignored.
Role
They tell the Xcode compiler to separate the code in the methods and functions pop-up menus at the top of the Editor pane, as shown in:
Some classes, especially some controller classes, can be long, and methods and function pop-up menus facilitate code navigation. Joining the #pragma directive at this point is very effective in logically organizing your code.
Attention
There must be no space behind the "-" #pragma mark–.
If your logo does not appear in the pop-up menu, such as if no divider appears, on the Xcode menu "Preferences ..." The "Code sense" option in the check box uncheck "Sort list Alphabetically".
#pragma (Introduction)
In all pre-processing directives, the #Pragma directive may be the most complex, and its role is to set the state of the compiler or to instruct the compiler to complete certain actions. #pragma指令对每个编译器给出了一个方法, given the unique characteristics of the host or operating system, while maintaining full compatibility with the C and C + + languages. By definition, the compilation instructions are proprietary to the machine or operating system and are different for each compiler.
The format is generally: #pragma Para. Where para is the parameter, here are some common parameters.
Message parameter
The message parameter can output the corresponding information in the compilation Information Output window, which is very important for the control of source code information. It is used in the following ways:
#pragma message ("text")
When the compiler encounters this instruction, it prints the message text in the compilation Output window.
When we define a number of macros in the program to control the source code version, it is possible for us to forget that we have the correct settings for these macros, and we can use this instruction to check at compile time. Let's say we want to determine if we have any definition of _x86 in the source code. This macro can be used in the following way
#ifdef _x86
#pragma message ("_x86 macro activated!")
#endif
When we define the _x86 macro, the application will be compiled with the "_x86 macro activated!" displayed in the Compile Output window. ”。 We will not scratching because we do not remember some of the specific macros we have defined.
Code_seg
Another pragma parameter that is used more is code_seg. Formats such as:
#pragma code_seg (["Section-name" [, "Section-class"]]
It is able to set the code snippet where the function code is stored in the program, which is used when we develop the driver.
#pragma once
(more commonly used)
Just by adding this instruction at the very beginning of the header file will ensure that the header file is compiled once, and this instruction is actually available in VC6, but given that compatibility does not have much to do with it.
#pragma once is a compilation-related, that is, this compilation system can be used, but in other compiled systems may not be, that is, the transplant is poor, but now basically every compiler has this definition.
#ifndef, #define, #endif这个是C + + language-related, which is a macro definition in the C + + language that avoids multiple compilations of files through macro definitions. So it works on all compilers that support the C + + language, and it's best to use this approach if you're writing programs that cross-platform
#pragma hdrstop
#pragma hdrstop indicates that the precompiled header file ends, and the subsequent header files are not precompiled. BCB can precompile the header file to speed up the link, but if all the header files are precompiled and may take up too much disk space, use this option to exclude some header files.
Sometimes there are dependencies between units, such as cell A depends on unit B, so unit B is compiled before unit A. You can use #pragma startup to specify the compilation priority, and if you use the #pragma package (smart_init), the BCB will be compiled according to the priority size.
#pragma resource
#pragma resource ". DFM "means toThe resources in the. dfm file are added to the project. The *.DFM includes the definition of the form's appearance.
#pragma warning
#pragma warning (disable:4507; once:4385; error:164)
Equivalent to:
#pragma warning (disable:4507 34)//Do not display 4507 and 34th warning messages
#pragma warning (once:4385)//No. 4385 warning information is reported only once
#pragma warning (error:164)//Put the 164th warning message as an error.
This pragma warning also supports the following formats:
#pragma warning (push [, N])
#pragma warning (POP)
Here n represents a warning level (1-4).
#pragma warning (push) to save an existing warning state for all warning messages.
#pragma warning (push, N) holds the existing warning state for all warning messages and sets the global warning level to N.
#pragma warning (pop) pops the last warning message to the stack,
All changes made between the stack and the stack are canceled. For example:
#pragma warning (push)
#pragma warning (disable:4705)
#pragma warning (disable:4706)
#pragma warning (disable:4707)
.......
#pragma warning (POP)
At the end of this code, all warning messages (including 4705,4706 and 4707) are re-saved.
pragma comment
pragma comment (...)
This directive places a note record into an object file or executable file.
The commonly used LIB keyword can help us to connect to a library file.
Each compiler can use the #pragma directive to activate or terminate some of the compilation features supported by the compiler. For example, for the Loop optimization feature:
#pragma loop_opt (ON)//Active
#pragma loop_opt (off)//termination
Sometimes, there are some functions in the program that will cause the compiler to emit warnings that you are familiar with and want to ignore, such as "Parameter xxx is never used in function xxx", you can:
#pragma warn-100//Turn off the warning message for warning #100
int Insert_record (REC *r)
{ }
#pragma warn +100//Turn The warning message for warning #100 back on
The function generates a warning message with a unique signature of 100, so that the warning can be temporarily terminated.
Each compiler has a different implementation of #pragma, which is effectively in a compiler that is almost invalid in another compiler. Can be viewed from the compiler's documentation.
#pragma pack (n) and #pragma pop ()
struct sample
{
Char A;
Double b;
};
When the sample structure does not add #pragma pack (n), sample is aligned to the largest member;
(The so-called alignment means that when the alignment number is n, each member is aligned, and if the size of member A is less than N, a is expanded to n size;
If the size of a is greater than N, the size of a is used, so the size of the above structure is 16 bytes.
When the sample structure is added #pragma pack (1), sizeof (sample) = 9 bytes; no null bytes.
(Note: When n is greater than the size of the largest member of the sample structure, n takes the maximum member size.)
So the larger the N, the faster the structure, the larger the size; conversely)
#pragma pop () is the meaning of canceling #pragma pack (n), which means that the next structure is not #pragma pack (n)
#pragma comment (comment-type, ["commentstring"])
Comment-type is a predefined identifier that specifies the type of annotation and should be one of the compiler,exestr,lib,linker.
Commentstring is a string that provides additional information for Comment-type.
Note Type:
1, compiler:
Place the compiler's version or name into an object file, which is ignored by linker.
2, Exestr:
In future versions will be canceled.
3. Lib:
Place a library search record into the object file, this type should be and commentstring (specify the name and path of the lib you want to linker search) The name of this library is placed behind the default library search record of the object file. Linker Search This library as you would enter this command at the command line. You can set up multiple library records in one source file, in the same order in the object file as in the source file. If the order of the default libraries and additional libraries is different, the z-compile switch is used to prevent the default libraries from being placed into the object module.
4, Linker:
Specify a connection option so that it does not have to be entered at the command line or set in the development environment.
Only the following linker option can be passed to linker.
/defaultlib,/export,/include,/manifestdependency,/merge,/section
(1)/defaultlib:library
The/DEFAULTLIB option adds a library to the list of libraries that LINK searches for when resolving references. The library specified by/defaultlib is searched after the library specified on the command line and the default library specified in the. obj file. Override/defaultlib:library for all default library (/NODEFAULTLIB) options. If the same library name is specified in both, the Ignore library (/nodefaultlib:library) option overrides/defaultlib:library.
(2)/export:entryname[, @ordinal [, Noname]][,data]
With this option, you can export a function from a program so that other programs can call the function. You can also export data. Exports are typically defined in a DLL. EntryName is the name of the function or data item to be used by the calling program. ordinal specifies an index of 1 to 65,535 in the export table, and if no ordinal is specified, LINK assigns one. The Noname keyword only exports the function as an ordinal, without entryname.
The data keyword specifies that the export item is an item. Data items in the client program must be declared with extern __declspec (dllimport).
There are three ways to export definitions, followed by the recommended order of use:
The exports statement in the __declspec (dllexport). def file in the source code the/EXPORT specification in the link command all of these three methods can be used in the same program. LINK also creates an import library when the program that contains the export is generated, unless the. exp file is used in the build.
LINK uses the decorated form of an identifier. The compiler modifies the identifier when creating the. obj file. If EntryName is assigned to the linker in its unmodified form (as in the source code), link attempts to match the name. If a unique matching name cannot be found, LINK issues an error message. When you need to assign an identifier to the linker, use the Dumpbin tool to get the decorated name of the identifier.
(3)/include:symbol
The/INCLUDE option notifies the linker to add the specified symbol to the symbol table.
To specify multiple symbols, type a comma (,), semicolon (;)) between the symbol names. or spaces. On the command line, specify/include:symbol once for each symbol.
The linker parses the symbol by adding the object that contains the symbol definition to the program. This feature is useful for adding library objects that are not linked to a program. Use this option to specify that the symbol will override the removal of the symbol through/OPT:REF.
We often use this kind of #pragma comment (lib, "*.lib"). #pragma comment (lib, "ws2_32.lib") means that the link ws2_32.lib this library. And in the project Setup to write a link into the ws2_32.lib effect, but this method of writing the program others in the use of your code when you do not have to set up the project settings
#pragma data_seg
#pragma data_seg introduction [1]
Use #pragma data_seg to create a new data segment and define shared data in the following format:
#pragma data_seg ("Shareddata")
HWND sharedwnd=null;//Shared data
#pragma data_seg ()
1, #pragma data_seg () is generally used in DLLs. In other words, a shared named data segment is defined in the DLL. The key is that the global variables in this data segment can be shared by multiple processes, or the global variables in the DLL cannot be shared between multiple processes.
2, the shared data must be initialized, otherwise the Microsoft compiler will put the data that is not initialized. BSS segment, resulting in shared behavior between multiple processes failing. For example
#pragma data_seg ("MyData")
int g_value; Note that the global isn't initialized.
#pragma data_seg ()
A DLL provides two interface functions:
int GetValue ()
{
return g_value;
}
void SetValue (int n)
{
G_value = n;
}
Then start two processes A and b,a and b all call this DLL, if a calls the SetValue (5); b then call int m = GetValue (); Then the value of M is not necessarily 5, but an undefined value. Because the global data in the DLL is private and cannot be shared for each process that invokes it. If you initialize the G_value, the G_value will surely be placed in the MyData section. In other words, if a calls the SetValue (5); b then call int m = GetValue (); Then the value of M must be 5, which enables data communication across processes.
Edit this paragraph
Application examples
In network protocol programming, data packets of different protocols are often processed. One method is to offset by the pointer
method to get a variety of information, but it's not only complicated programming, but once the protocol changes, the program changes.
Also more trouble. After understanding the compiler's principle of allocation of structure space, we can fully utilize this
A feature defines its own protocol structure that accesses the members of the structure to obtain various information. Do this,
Not only simplifies programming, but even if the protocol changes, we only need to modify the definition of the protocol structure,
Other programs without modification, save time and effort. The following is an example of the TCP protocol header, which describes how to define the protocol structure.
The protocol structure is defined as follows:
#pragma pack (1)//aligned in 1-byte manner
struct Tcpheader
{
Short srcport; 16-bit Source port number
Short dstport; 16-bit Destination port number
int Serialno; 32-bit serial number
int ackno; 32 Digit Confirmation Number
unsigned char haderlen:4; 4-Bit header length
unsigned char reserved1:4; Keep 4 bits in 6 bits
unsigned char reserved2:2; Keep 2 bits in 6 bits
unsigned char urg:1;
unsigned char ack:1;
unsigned char psh:1;
unsigned char rst:1;
unsigned char syn:1;
unsigned char fin:1;
Short windowsize; 16-bit window size
Short tcpchksum; 16-bit TCP test and
Short Urgentpointer; 16-bit emergency pointer
};
#pragma pop ()//Cancel 1-byte alignment
#pragma the alignment length specified in the pack, the actual rules used are: struct, union, or data member of the class, the first place is placed at offset 0, and then the alignment of each data member, according to the value specified by #pragma pack and the length of the data member itself, is larger than the other. However, when the value of the #pragma pack is equal to or exceeds the length of the longest data member, the size of the value will have no effect. The alignment of the structure as a whole is done by the smaller one, which is the largest data member in the structure and the value specified by the #pragma pack.
Specify the library to use for the connection for example, when we connect, we use WSock32.lib, and you can certainly take the trouble to add it to your project. But I think the more convenient way is to use the #pragma indicator to specify the library to connect to: #pragma comment (lib, "WSock32.lib").
#pragma in Xcode to use