#pragma indicator application example

xiaoxiao2021-03-06  107

Although both C and C have standards, almost every compiler (broad, including connector, etc.) extends some C / C keywords. Apply these keywords reasonably, sometimes it makes our work very convenient. The use of the #pragma indicator in Visual C is explained below.

First, export DLL functions with #pragma

The traditional way to download the DLL function is to use the module definition file (.def), Visual C provides a more concise method, that is, "__DECLSPEC ()" keywords are followed by "DLLEXPORT", tell the connection to export this function ,E.g:

__DECLSPEC (DLLEXPORT) INT __STDCALL MYEXPORTFUNCTION (ITEST);

Place "__DECLSPEC (DLLEXPORT" on the front of the function declaration, the connection generated DLL exports the function "_myexportFunction @ 4".

The name of the above export function may not be my hope, we hope to export the original "MyExportFunction". Fortunately, VC provides a pre-processing indicator "#pragma" to specify the connection option (not just this feature, there are many indication functions), as follows:

#pragma comment (linker, "/ export: myexportfunction = _myexportfunction @ 4")

This is like a person who is willing to :). If you want to specify the order of the export, or only export the function as the serial number, no entryname, this pre-processing indicator (exactly the connector) can be implemented, see the MSDN's grammatical description:

/ Export: entryname [, @ ordinal [, noame]] [, DATA]

@ordinal specified sequence; Noname Specifies that only the function is exported as a serial number; the DATA keyword specifies the export item as a data item.

Second, the instruction file only contains one time

In the header file, we are generally enough to include once, but if I have a header file in multiple.c / .cpp files, such as Windows.h, many declarations, etc. Two times? The traditional way to solve this problem is to define a macro in the header file, such as Windows.h:

#ifndef _windows_ # define _Windows_

Then, in the file, the file is added to #ENDIF, so it can avoid being included multiple times. But such consequences are the readability of the code (personal view), VC gives us another way, that is, in front of the file:

#pragma overce

Is it very convenient? Third, the warning is invalid. Sometimes we have to force the variables, thereby attracting a warning of the compiler, especially in C , the type check is more stringent with C. Although this does not affect what, it seems uncomfortable - I deliberately want this, what do you warn! :) At this time you see the warning type, such as "Warning C4311:" Type Conversion ": From" HHOOK "to" Bool "pointer truncation, add:

#pragma Warning (Disable: 4311)

The compiler has nothing to say :). Fourth, specifying the library to be used for use with WSOCK32.LIB when we connect, you can of course join it to your project. But I think more convenient is to use the #pragma indicator, specify the library to be connected: #pragma comment (lib, "wsock32.lib")

5. Show compilation news is not much used, give an example:

#ifdef _debug # pragma Message ("Compile Connection to Debug Mode ...") # Endif //_Debug

In fact, "#pragma" features are far more than this, but the above is just a horns of the iceberg. Specific use method, detailed, clear, see MSDN, here is just talking about these features :)

转载请注明原文地址:https://www.9cbs.com/read-100628.html

New Post(0)