#pragma的作用

#pragma

The #pragma command gives the programmer the ability to tell the compiler to do certain things. Since the #pragma command is implementation specific, uses vary from compiler to compiler. One option might be to trace program execution.

 

//MSDN-描述

Pragma directives specify machine- or operating-specific compiler features. The __pragma keyword, which is specific to the Microsoft compiler, enables you to code pragma directives within macro definitions.

#pragma token-string
__pragma(token-string)

Remarks

Each implementation of C and C++ supports some features unique to its host machine or operating system. Some programs, for example, must exercise precise control over the memory areas where data is put or to control the way certain functions receive parameters. The #pragma directives offer a way for each compiler to offer machine- and operating system-specific features while retaining overall compatibility with the C and C++ languages.

Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler. Pragmas can be used in conditional statements, to provide new preprocessor functionality, or to provide implementation-defined information to the compiler.

The token-string is a series of characters that gives a specific compiler instruction and arguments, if any. The number sign (#) must be the first non-white-space character on the line that contains the pragma; white-space characters can separate the number sign and the word "pragma". Following #pragma, write any text that the translator can parse as preprocessing tokens. The argument to #pragma is subject to macro expansion.

If the compiler finds a pragma that it does not recognize, it issues a warning and continues compilation.

The Microsoft C and C++ compilers recognize the following pragmas:

alloc_text

auto_inline

bss_seg

check_stack

code_seg

comment

component

conform1

const_seg

data_seg

deprecated

fenv_access

float_control

fp_contract

function

hdrstop

include_alias

init_seg1

inline_depth

inline_recursion

intrinsic

make_public

managed

message

omp

once

optimize

pack

pointers_to_members1

pop_macro

push_macro

region, endregion

runtime_checks

section

setlocale

strict_gs_check

unmanaged

vtordisp1

warning

1. Supported only by the C++ compiler.

Pragmas and Compiler Options

Some pragmas provide the same functionality as compiler options. When a pragma is encountered in source code, it overrides the behavior specified by the compiler option. For example, if you specified /Zp8, you can override this compiler setting for specific sections of the code with pack:

Copy Code

cl /Zp8 ...

<file> - packing is 8
// ...
#pragma pack(push, 1) - packing is now 1
// ...
#pragma pack(pop) - packing is 8
</file>
The __pragma() Keyword

Microsoft specific

The compiler also supports the __pragma keyword, which has the same functionality as the #pragma directive, but can be used inline in a macro definition. The #pragma directive cannot be used in a macro definition because the compiler interprets the number sign character ('#') in the directive to be the stringizing operator (#).

The following code example demonstrates how the __pragma keyword can be used in a macro. This code is excerpted from the mfcdual.h header in the ACDUAL sample in "Compiler COM Support Samples":

Copy Code

#define CATCH_ALL_DUAL \
	CATCH(COleException, e) \
{ \
	_hr = e->m_sc; \
} \
	AND_CATCH_ALL(e) \
{ \
	__pragma(warning(push)) \
	__pragma(warning(disable:6246)) /*disable _ctlState prefast warning*/ \
	AFX_MANAGE_STATE(pThis->m_pModuleState); \
	__pragma(warning(pop)) \
	_hr = DualHandleException(_riidSource, e); \
} \
	END_CATCH_ALL \
	return _hr; \

End Microsoft specific

转载于:https://www.cnblogs.com/agpro/archive/2010/06/11/1756414.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值