关于Pragma

 1 /** This is a introduction of how to use pragma. */
 2
 3 #pragma once /// This is used for include the header once.
 4 /**
 5 Note that this is certain for VS compiler but not certain for other compilers that not support this.
 6 As this is not an orginal C++ standard.
 7 Use
 8
 9 #ifndef _GUARD_
10 #define _GUARD_
11 #endif // _GUARD_
12
13 for a portable program. And if your environment is allowed use #pragma once for first option.
14 Casue #ifndef-#endif will make preprocessor symbol which will ended in symbol collisions and pollute global namespace.
15 */
16
17 #pragma comment( [type], "name" ) /// This is to tell the compiler to add certain contents in .obj files.
18 /**
19 for type
20 \compiler put the compiler version and name in .obj, which will be ignored by compiler.
21 i.o
22 #pragma comment( compiler ) // no "name" input, or error caused.
23
24 \exestr put the "name" inside .obj won`t be loaded in memory but will be searched by dumpbin.
25 i.p
26 #pragma comment( exestr, "versionCode" ) // you can use this to embeded the version code in your exe files.
27
28 \lib use this to add lib in your projects.
29 i.o
30 #pragma comment( lib, "Gdiplus" ) // which add the Gdiplus.lib for compiler.
31
32 \linker put link file in your project instead of command line and environment settings.
33 i.o
34 #pragma comment( linker, "/include:__mySymbol" ) // use /include to force adding in.
35 There are also other options
36 /DEFAULTLIB /EXPORT /INCLUDE /MERGE /SECTION see msdn for details
37
38 \user put the "name" inside .obj, which will be ignored by compiler.
39 i.o
40 #pragma comment( user, "Compiled on"__DATE__" at "__TIME__ ) // put the compile date in .obj
41 */
42
43 #pragma message( "message text" ) /// This will output text when your compiler process to that part.
44 /**
45 In which, this is useful for you to know which kind of header file is included
46 when you are programming in multi-environments.
47 */
48
49 #pragma code_seg( [ push | pop ], [ identifier ], "segment-name" | "segment-class" ) /// Define the segment in .obj files.
50 /**
51 By default, the segment of a function is put in .text segment.
52 \push add an record in the stack in compiler by record name or segment.
53 \pop take the top of the stack in compiler by record name or segment.
54 \identifier while push, this make you push a record name.
55 i.o
56 #pragma code_seg( push, r1, "textOne" )
57 Which push the function under as the record r1 under .text segment named .textOne
58 . */
59
60 #pragma hdrstop /// Indicate that the pre-compile ended here.
61 /**
62 So you can use this to make some head files pre-compiled.
63 Or you may change the priority of some files whne you used #pragma package( smart_init )
64 */
65
66 #pragma warning( [ warning-specifier : warning-number-list ]; [ warning-specifier : warning-number-list ] ) /// Modify the warning for compiler.
67 /**
68 for waring-specifier
69 \once warn only once
70 \disable disable warning
71 \default reset to default level
72 \error make warning info as error
73 i.o
74 #pragma warning( disable: 4507 64; once: 4385; error: 164 )
75 #pragma warning( pop ) // pop the last warning info in stack and other modify before this command canceled.
76 . */
77
78 #pragma auto_inline( [ on | off ] ) /// Turn on or of the autoinline function for compiler.
79
80 #pragma inline_depth( [ 0...255 ] ) /// Used for those function marked inline or _inline to decide times of the function calls expand.
81
82 #pragma inline_recursion( [ 0...255 ] ) /// Used for those function marked inline or _inline to decide times of the recursive function calls expand.
83 /** Note that this require /Ob option setted to 1 or 2 for compiler. */
84
85 #pragma init_seg( [ compiler | lib | user ], "section-name" | "func-name" ) /// Specifies a keyword or code section that affects the order in which startup code is executed.
86 /**
87 This is useful for 3rd-party dlls whick requiring initialization.
88 \compiler remain Microsof C run-time order. Constructed first.
89 \lib marked as compiler and before any others.
90 \user available to any users but construted last.
91 */

以上是从网上搜集的关于 #pragma 的一些用法和注意事项,还参考了部分的 msdn.

个人觉得最常用的还是 #pragma once 和 #pragma comment 不过真的碰到多环境的情况用一用 #pragma message 也不错XD.

时间: 2024-08-06 07:57:31

关于Pragma的相关文章

swift pragma mark

众所周知,大家在OC中对代码进行逻辑组织 用的是#pragma mark - ,生成分隔线 用#pragma mark 函数说明,来生成一个函数的说明X 但在swift中,这个语法就不支持了,毕竟它是属于C的语法,于是就有了新的一些语法,如:// MARK: // FIXME // TODO: 等 // MARK: - 生成分隔线 // MARK: 说明 别忘了那个冒号... 参考 :http://stackoverflow.com/questions/24017316/pragma-mark-

C++ #pragma 预处理指令

#pragma 预编译指令的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的情况下,给出主机或操作系统专有的特征. 其使用的格式一般为: #pragma Para.其中Para 为参数,常见的参数如下: (1)Message参数 Message参数编译信息输出窗口中输出相应地信息,使用方法如下: #pragma message("消息文本") 使用示例,假如在程序中我们定义了很多宏来控制源代码版本的

[C++]关于头文件中的防卫式声明(#ifndef...#pragma once)

大家知道,我们写.h文件时,通常会加上防卫式声明,有以下两种方式: 1. 宏定义 #ifndef _FILENAME_ #define _FILENAME_ //... #endif 2. 编译器指令 #pragma once 但是,为什么头文件中需要添加这种防卫式声明呢?如果没有这样的声明,会出现怎样的问题.这里,先看一个例子. -- "Car.h",代码如下(并没有添加防卫式声明): // Car.h class Car { // ... }; -- "Person.h&

#pragma预处理命令

#pragma预处理命令 #pragma可以说是C++中最复杂的预处理指令了,下面是最常用的几个#pragma指令: #pragma comment(lib,"XXX.lib") 表示链接XXX.lib这个库,和在工程设置里写上XXX.lib的效果一样. #pragma comment(linker,"/ENTRY:main_function") 表示指定链接器选项/ENTRY:main_function #pragma once 表示这个文件只被包含一次 #pra

关于C++代码中的#pragma预处理指令

预处理指令是指在编译器编译代码时,提供按条件跳过源文件中的代码段(节).报告错误(错误信息以及行号)和警告条件,以及描绘源代码的不同区域的能力. 总是占用源代码中的单独一行,并且总是以 # 字符和预处理指令名称开头.# 字符的前面以及 # 字符与指令名称之间可以出现空白符. 下面是可用的预处理指令: #define 和 #undef,分别用于定义和取消定义条件编译符号. #if.#elif.#else 和 #endif,用于按条件跳过源代码中的节. #line,用于控制行号(在发布错误和警告信息

#pragma warning (default : n)

参考链接:http://www.cnblogs.com/JCSU/articles/1996483.html 在VC2013中编译以下win32 C++ 控制台程序,会产生2个告警warnings #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]){    int x, y, z;    y = x;    //return 0;} 警告    1    warning C4101: “z”: 未引用的局部变量 错误    

#pragma comment(转)

此文转自微软MSDN.注意这是在Windows上才有的,Linux上可没有. #pragma comment( comment-type [,"commentstring"] ) 备注 comment-type 是一个预定义的标识符(如下所述),它指定了注释记录的类型. 可选 commentstring 是一个字符串,它提供了某些注释类型的附加信息. 由于commentstring 是一个字符串,因此它遵循有关转义字符.嵌入的引号 (") 和串联的字符串的所有规则.  1.

14.C#属性访问器、命名空间、pragma指令(七章7.3-7.5)

看到一些零星的知识片,今天就用自己的理解说明下,也是因为太简单了,一下就过的,也是我们日常开发中常用.留下一个脚印,当书不在手上的,也能翻出来看看.说下属性访问器.命名空间和pragma指令. 属性访问器在01.C#数据类型.排序.过滤(一章1.1-1.2)有所提到,在C#3后可以使用修饰符去修饰属性的取值和赋值,也可以使用加入一些验证,如下: 1 class Plant 2 { 3 private double Height = 0.0; 4 5 //是否需要修剪 6 public bool

善用#waring,#pragma mark 标记

在项目开发中,我们不可能对着需求一口气将代码都写好.开发过程中肯定遇到诸如需求变动,业务逻辑沟通,运行环境的切换等这些问题.当项目大的时候,如果木有形成统一的代码规范,在项目交接和开发人员沟通上将会带来很大的麻烦. #pragma mark - 这个标记在iOS开发中用得最多了.其实最主要的是用来进行标记的,当然也有注释的作用在里面.当然我们也可以用//,/* */等常用注释来说明.但是用#pragma mark -不同的是可以将整个文件的函数以类似分组的形式展现.当我们点击Xcode 导航栏上

#pragma

once 头文件被编译一次.就能够保证头文件只被编译一次 warning (disable:1111) 不报 ( once:1111)报一次 ( error:1111)报 comment  传统的到出 DLL 函数的方法是使用模块定义文件 (.def),Visual C++ 提供了更简洁方便的方法, 那就是“__declspec()”关键字后面跟“dllexport”,告诉连接去要导出这个函数,例如: __declspec(dllexport) int __stdcall MyExportFun