BCB6中的#Pragma说明(一)
1.#pragma alignment
alignment pragma用于显示一条当前内存对齐(alignment)和枚举(enum)字节大小的消息。例如:
W8035: The alignment is 8 bytes, the enum size is 4 bytes
该消息必须在打开编译器选项“show general messages”或编译器开关“-wmsg”后才能显示。
2.#pragma anon_struct
#pragma anon_struct on 允许编译类中内嵌的匿名结构
#pragma anon_struct off
禁止编译类中内嵌的匿名结构
例如:
#pragma anon_struct on
struct S
{
int i;
struct // Embedded anonymous struct
{
int j ;
float x ;
};
S() { i = 1; }
};
#pragma anon_struct off
3.#pragma argsused
argsused pragma通常在定义一个函数前使用,用于屏蔽函数参数未使用的警告。例如:
#pragma argsused
int func(int a,int b)
{
++b;
return b;
}
如果不使用该pragma,编译器就会报类似“函数参数没有用到”的警告消息。
4.#pragma checkoption optionstring
checkoption pragma检查optionstring中包含的选项是否已设置。例如:
#pragma checkoption -a4 –b
如果没有设置,显示一个错误:E2471: pragma checkoption failed: options are not as expected。
5.#pragma codeseg
#pragma codeseg <seg_name> <"seg_class"> <group>
codeseg pragma用于重命名函数代码所在的段(segment)、类(class)、组(group)。如果codeseg后没有任何选项,表示使用缺省代码段。
6.#pragma comment
#pragma comment (comment type, "string")
comment pragma用于将一个comment记录写入到输出文件。Comment类型可以下列值之一:
exestr
编译器将string写入到一个.obj文件中,最终放置到可执行文件中。使用文件搜索工具可以从可执行文件中找到该string。
lib
连接器将一个string中指定的.lib文件写入到一个.obj文件中。
user
编译器将string写入到一个.obj文件中,但连接器忽略,就是说最终string不放置到可执行文件中。
7.#pragma defineonoption and #pragma undefineonoption
#pragma defineonoption name switch(es)
#pragma undefineonoption name switch(es)
这两个pragma用于为若干个命令行开关建立一个别名(name)。除了部分负责输入的命令行开关(如-I、-L)外,大部分命令开关都可以在这两个指示符中使用。
#pragma defineonoption在别名中所有指定开关都已打开的情况下定义name。
#pragma undefineonoption在别名中所有指定开关都已打开的情况下不定义name。
例如,myfile.c文件包含下列代码:
#pragma defineonoption OPTIMIZING -O2
#ifdef
OPTIMIZING
#pragma message We are optimizing with -O2
#else
#pragma message There are no optimizations turned on
#endif
bcc32 -c myfile.c,输出是There are no optimizations turned on
bcc32 -c -O2 myfile.c,输出是We are optimizing with -O2
下面是一个同时使用多个命令行开关的实例:
#define SWITCHES_ARE_OFF
#pragma undefineonoption SWITCHES_ARE_OFF -O2 -c -P -C
#ifdef
SWITCHES_ARE_OFF
#pragma message At least one of the switches is not turned on
#else
#pragma message All switches are turned on
#endif
bcc32 -c myfile.c,输出是At least one of the switches is not turned on
bcc32 -c -P -O2 -C,输出是All switches are turned on
8. #pragma exit and #pragma startup
#pragma startup function-name <priority>
#pragma exit function-name <priority>
这两个pragma分别用于指定在main函数调用前调用和在_exit函数调用前调用的函数。函数定义格式如下:
void func(void);
priority参数数值范围为64-255,0值级别最高。级别越高,在main函数调用前越早调用,在-exit函数调用前越晚调用。缺省级别为100。0-63级别C库保留使用。
9.#pragma hdrfile
#pragma hdrfile "filename.CSM"
hdrfile pragma用于指定预编译头文件的名称。前提是必须已启用预编译头文件功能。