本站首页    管理页面    写新日志    退出                                          --==~欢迎光临FoxWolf的Blog~==--   



 日志搜索


«September 2025»
123456
78910111213
14151617181920
21222324252627
282930


公告


我的分类

日志更新

最新评论

留言板

链接

 


Blog信息
blog名称:FoxWolf
日志总数:127
评论数量:246
留言数量:0
访问次数:855287
建立时间:2006年5月31日




[c/c++]#pragma
文章收藏,  软件技术,  电脑与网络

FoxWolf 发表于 2007/6/11 13:38:11

  #pragma   data_seg(".mdata").....#pragma   data_seg()可以让编译器把两者之间的所有已初始化变量放入一个新的.mdata段中    #pragma   once的意思是要求编译器在编译过程中只将包含此命令文件编译(打开)一次,从而避免重复包含此文件 Pragma是什么?翻译:Rogers后附英文原文。(译者注:一句话,pragma就是为了让编译器编译出的C或C++程序与机器硬件和操作系统保持完全兼容而定义的宏扩展,#pragma是和特定编译器相关的。)两部分:1.Pragma说明;2.Pragma的语法。一、Pragma说明(Directives)C和C++程序的每次执行都支持其所在的主机或操作系统所具有的一些独特的特点。一些程序,例如,需要精确控制数据存放的内存区域或控制某个函数接收的参数。#pragma指示为编译器提供了一种在不同机器和操作系统上编译以保持C和C++完全兼容的方法。?Pragmas是由机器和相关的操作系统定义的,通常对每个编译器来说是不同的。二、语法(Syntax)#pragma token-string(特征字符串)特征字符串是一连串的字符,就是要给一个特定编译器提供说明和编译意见。符号(#)必须是pragma所在那一行的第一个非空格字符;#号和pragma之间可以有任意个空格符。在#pragma之后,是可以被编译器解析的预处理特征字符。一般认为,#pragma属于宏扩展。如果编译器发现不认识的pragma,会提出警告,但继续编译下去。Pragmas可以用在条件声明上,提供最新的功能性的预处理程序,或者提供给编译器定义执行的信息。C和C++编译器认可如下pragmas:alloc_textcomment //注释init_seg1  optimize  //最优化auto_inline component  //组成部件inline_depth pack       //包bss_seg data_seg inline_recursion  //内嵌递归pointers_to_members1 check_stack function    intrinsic  //内在的setlocale code_seg hdrstop message  vtordisp1 const_seg include_alias once warning 这是MSDN的一篇文章,原作者曾经想使用 #pragma pack(1) // 用GCC在MIPS平台上将结构体成员结合到一块连续的内存块,但是没有做到。在linux环境下使用intel-based GCC,#pragma pack(1)可以工作。建议参考具体编译器的文档,在里面应该有pragma的说明。---------------------------------------原文-------------------------------------------Pragma DirectivesEach implementation of C and C++ supports some features unique to its host machine or operating system.Some programs, for instance, need to exercise precise control over the memory areas where data is placed or to control the way certain functions receive parameters. The #pragma directives offer a way for each compilerto 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.Syntax#pragma token-stringThe 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 containing 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 it does not recognize, it issues a warning, but compilation continues.Pragmas can be used in conditional statements, to provide new preprocessor functionality, or to provide implementation-defined information to the compiler. The C and C++ compilers recognize the following pragmas:alloc_text comment init_seg1 optimize auto_inline component inline_depth pack bss_seg data_seg inline_recursion pointers_to_members1 check_stack function intrinsic setlocale code_seg hdrstop message vtordisp1 const_seg include_alias once warning This is an article from MSDN, I ever wanted to use#pragma pack(1) //which can combine structure members to one continuous memory blockon MIPS platform using GCC, but it doesn't work.See the compiler's document, it should be illustrated there.Under linux env using intel-based GCC, it works.   在所有的预处理指令中,#Pragma  指令可能是最复杂的了,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作。#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的情况下,给出主机或操作系统专有的特征。依据定义,编译指示是机器或操作系统专有的,且对于每个编译器都是不同的。  其格式一般为: #Pragma  Para  其中Para  为参数,下面来看一些常用的参数。   (1)message  参数。  Message  参数是我最喜欢的一个参数,它能够在编译信息输出窗  口中输出相应的信息,这对于源代码信息的控制是非常重要的。其使用方法为:   #Pragma  message(“消息文本”)   当编译器遇到这条指令时就在编译输出窗口中将消息文本打印出来。  当我们在程序中定义了许多宏来控制源代码版本的时候,我们自己有可能都会忘记有没有正确的设置这些宏,此时我们可以用这条指令在编译的时候就进行检查。假设我们希望判断自己有没有在源代码的什么地方定义了_X86这个宏可以用下面的方法   #ifdef  _X86   #Pragma  message(“_X86  macro  activated!”)   #endif   当我们定义了_X86这个宏以后,应用程序在编译时就会在编译输出窗口里显示“_  X86  macro  activated!”。我们就不会因为不记得自己定义的一些特定的宏而抓耳挠腮了  。   (2)另一个使用得比较多的pragma参数是code_seg。格式如:  #pragma  code_seg(  ["section-name"[,"section-class"]  ]  )  它能够设置程序中函数代码存放的代码段,当我们开发驱动程序的时候就会使用到它。    (3)#pragma  once  (比较常用)  只要在头文件的最开始加入这条指令就能够保证头文件被编译一次,这条指令实际上在VC6中就已经有了,但是考虑到兼容性并没有太多的使用它。   (4)#pragma  hdrstop表示预编译头文件到此为止,后面的头文件不进行预编译。BCB可以预编译头文件以加快链接的速度,但如果所有头文件都进行预编译又可能占太多磁盘空间,所以使用这个选项排除一些头文件。  有时单元之间有依赖关系,比如单元A依赖单元B,所以单元B要先于单元A编译。你可以用#pragma  startup指定编译优先级,如果使用了#pragma  package(smart_init)  ,BCB就会根据优先级的大小先后编译。  (5)#pragma  resource  "*.dfm"表示把*.dfm文件中的资源加入工程。*.dfm中包括窗体  外观的定义。   (6)#pragma  warning(  disable  :  4507  34;  once  :  4385;  error  :  164  )  等价于:  #pragma  warning(disable:4507  34) //  不显示4507和34号警告信息  #pragma  warning(once:4385) //  4385号警告信息仅报告一次  #pragma  warning(error:164) //  把164号警告信息作为一个错误。  同时这个pragma  warning  也支持如下格式:  #pragma  warning(  push  [  ,n  ]  )  #pragma  warning(  pop  )  这里n代表一个警告等级(1---4)。  #pragma  warning(  push  )保存所有警告信息的现有的警告状态。  #pragma  warning(  push,  n)保存所有警告信息的现有的警告状态,并且把全局警告  等级设定为n。 #pragma  warning(  pop  )向栈中弹出最后一个警告信息,在入栈和出栈之间所作的  一切改动取消。例如:  #pragma  warning(  push  )  #pragma  warning(  disable  :  4705  )  #pragma  warning(  disable  :  4706  )  #pragma  warning(  disable  :  4707  )  //.......  #pragma  warning(  pop  ) 在这段代码的最后,重新保存所有的警告信息(包括4705,4706和4707)。  (7)pragma  comment(...)   该指令将一个注释记录放入一个对象文件或可执行文件中。  常用的lib关键字,可以帮我们连入一个库文件。  每个编译程序可以用#pragma指令激活或终止该编译程序支持的一些编译功能。例如,对循环优化功能:  #pragma  loop_opt(on) //  激活  #pragma  loop_opt(off) //  终止  有时,程序中会有些函数会使编译器发出你熟知而想忽略的警告,如“Parameter  xxx  is  never  used  in  function  xxx”,可以这样:  #pragma  warn  —100 //  Turn  off  the  warning  message  for  warning  #100  int  insert_record(REC  *r)  {  /*  function  body  */  }  #pragma  warn  +100 //  Turn  the  warning  message  for  warning  #100  back  on  函数会产生一条有唯一特征码100的警告信息,如此可暂时终止该警告。  每个编译器对#pragma的实现不同,在一个编译器中有效在别的编译器中几乎无效。可从编译器的文档中查看。


阅读全文(1677) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.023 second(s), page refreshed 144791764 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号