"C language" pragma

Source: Internet
Author: User

#pragma comment (lib, "Libgsl.a")

This tells the compiler to add a message to the compiled. obj file and the. exe file so that the linker is going to look for the LIBGSL.A library when it is linked to the library, not to find another library first.

After adding this statement, you do not need to add the Libgsl.a file to the project via the linker.

#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. The following article goes from a blog: http://www.cppblog.com/longzxr/archive/2010/08/07/122577.html

Use of #pragma

Although both C and C + + already have standards, almost every compiler (broadly speaking, including connectors, etc.) extends some C + + keywords.

The proper use of these keywords can sometimes make our work very convenient. Let's talk about Visual C + + #pragma

The use of indicators.

I. Exporting DLL functions with #pragma

The traditional approach to DLL functions is to use the module definition file (. def), which provides a more concise and convenient way to

That is the "__declspec ()" keyword followed by "dllexport", telling the connection to export this function, for example:


__declspec (dllexport) int __stdcall myexportfunction (int iTest);

Put "__declspec (dllexport)" At the top of the function declaration, and the DLL that is generated by the connection will export the function

"[email protected] ".

The name of the exported function above may not be my hope, we want to export the original "Myexportfunction".

Fortunately, the VC provides a preprocessing indicator "#pragma" to specify the connection options (not just this one feature, there are many instructions),

As follows:


#pragma COMMENT (linker, "/export:[email protected]")

This is the day as People wish:). If you want to specify the order of the exports, or simply export the function as an ordinal, without Entryname,

This preprocessing indicator (to be sure the connector) can be implemented to see the MSDN syntax Description:


/export:entryname[, @ordinal [, Noname]][,data]

@ordinal the specified order; NONAME specifies that only the function is exported as an ordinal; the data keyword specifies that the export item is an item.

Second, the instruction file contains only one time

In the header file, it is usually enough for the entire project to be included once, but if I have to package in multiple. c/.cpp files

With a head file, such as Windows.h, is there a lot of statements and so on, two times? The traditional approach to solving this problem is in the header file

Start out with

#define Define a macro, such as Windows.h:



#ifndef _windows_#define _windows_


P> then adds #endif to the file, so you can avoid being included multiple times. But the consequence is that the code is less readable

(personal view), VC provides us with another way, that is, in front of the file add: #pragma once is not very convenient?

Third, invalidate the warning sometimes we have to cast the variable, which draws a warning from the compiler, especially in C + +

, type checking is more stringent than C. It doesn't make any difference, but it looks awful--I mean it.

What are you warning about! :) At this time you see the type of warning,

For example "Warning C4311:" type conversion ": pointer truncation from" Hhook "to" BOOL ", preceded by:

#pragma warning (disable:4311) The compiler has nothing to say:).

Iv. Specify the library to use for the connection we used the WSock32.lib when we connected, of course you can.

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") Five, display the compilation message is not much use, for example:

#ifdef _DEBUG#PRAGMA Message

("Compile connection for debug mode ...") #endif//_DEBUG

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 indicate

The compiler completes some specific actions. #pragma
Directive provides a method for each compiler to give a host or operating system exclusive if it remains fully compatible with the C and C + + languages

The characteristics. 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.

(1) Message parameter. The message parameter is one of my favorite parameters, it can be in the compilation Information Output window
It is important to control the source code information by outputting the appropriate information in the port. 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, we may forget that we have the correct

These macros, we can use this command at compile time to check. Suppose we want to judge whether we have the source

Where does the code define the

_X86 This macro can be used in the following ways
#ifdef _x86
#Pragma message ("_x86 macro activated!")
#endif
When we define the _x86 macro, the application will be compiled in the Compile Output window to display "_
X86 macro activated! ". We're not scratching because we don't remember some of the specific macros we've defined.

(2) 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.

(3) #pragma once (more commonly used)
As long as this command is added at the very beginning of the header file, it is guaranteed that the header file will be compiled once, and this instruction is actually in VC6.

But considering compatibility doesn't have much to do with it.

(4) #pragma hdrstop indicates that the precompiled header file ends, and the subsequent header files are not precompiled. BCB can precompile header files to add

Fast link speed, but if all header files are precompiled and may account for 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 specifies the compilation priority, if the #pragma package (smart_init) is used, the BCB is based on the priority

Size successively compiled.

(5) #pragma resource "*.DFM" means to add the resources in the *.DFM file to the project. Include form in *.DFM
The definition of the appearance.

(6) #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)//164th warning message as an error.
This pragma warning also supports the following format:
#pragma warning (push [, n])
#pragma warning (pop)
here n represents a warning level (1---4). The
#pragma warning (push) holds an existing warning state for all warning messages. The
#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, and the
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 warnin G (disable:4707)
//....
#pragma warning (pop)
to resave all warning messages (including 4705,4706 and 4707) at the end of this code.
(7) pragma comment (...)
The 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.



  #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 ,
Remarks:
1, Compiler: Place the compiler's version or name into an object file, which is ignored by linker.
2, Exestr: will be canceled in a later version.
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 search for liner)
The name of the library is placed behind the default library search record for 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 and attached libraries is
different, using the z-compile switch is to prevent the default library from being placed in the object module.
4, Linker: Specifies 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 you specify the same library name in both, the Ignore libraries (/NODEFAULTLIB:Library) option overrides the/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:

    1. __declspec (dllexport) in source code

    2. Exports statements in. def files

    3. The/EXPORT specification in the LINK command

All three of these 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 one/include: symbol 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.
As in the project setup, the same effect as the ws2_32.lib, but this method writes
Program others use your code when you don't have to set up the project settings

"C language" pragma

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.