Pragma是什么?

Pragma是什么?
翻译:SkyJacker
后附英文原文。

(译者注:
一句话,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_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

这是MSDN的一篇文章,原作者曾经想使用
#pragma pack(1) // 用GCC在MIPS平台上将结构体成员结合到一块连续的内存块,但是没有做到。

在linux环境下使用intel-based GCC,#pragma pack(1)可以工作。

建议参考具体编译器的文档,在里面应该有pragma的说明。

---------------------------------------原文-------------------------------------------

Pragma Directives
Each 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 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.

Syntax

#pragma token-string

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 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 block
on 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是什么?相关推荐

  1. IOS笔记 #pragma mark的用法

    简单的来说就是为了方便查找和导航代码用的. 下面举例如何快速的定位到我已经标识过的代码. #pragma mark 播放节拍器 - (void) Run:(NSNumber *)tick { //.. ...

  2. #pragma pack(n) 的作用

    在C语言中,结构是一种复合数据类型,其构成元素既可以是基本数据类型(如int.long.float等)的变量,也可以是一些复合数据类型(如数组.结构.联合等)的数据单元.在结构中,编译器为结构的每个成 ...

  3. 【数据库】sqlite中PRAGMA命令说明

    关于sqlite中PRAGMA的说明网上已经有很多了,这里不再复制粘贴,只把内容最全的网址连接记录一下: 官网说明(英文):https://sqlite.org/pragma.html 中文翻译,参见 ...

  4. #pragma pack 内存对齐

    1. 用法 #pragma pack是C/C++里面设置字节对齐方式的预编译函数 2. 解释 先看下面代码: #include <iostream> using namespace std ...

  5. #pragma once与#ifndef的区别

    #pragma once 和 #ifdef 都可以避免同一个文件被include多次,但二者的含义略有不同. 1. #pragma once方式 1.1. 使用方法 #pragma once// co ...

  6. #pragma once与#ifndef

    在C/C++中,在使用预编译指令#include的时候,为了防止重复引用造成二义性的两种方法. #ifndef 它不光可以保证同一份文件不会被包含两次,也能够保证不同文件完全相同的内容不会被包含两次. ...

  7. C/C++ 头文件 #pragma once vs #ifndef

    C/C++ 头文件防止被重复包含有2种常用的做法,一种是传统的ifndef: #ifndef HEADER_H #define HEADER_H...#endif // HEADER_H 另一种是稍微 ...

  8. #pragma once与 #ifndef的区别

    2019独角兽企业重金招聘Python工程师标准>>> 为了避免同一个文件被include多次 1   #ifndef方式 2   #pragma once方式 在能够支持这两种方式 ...

  9. #pragma once 是什么意思?

    和头文件中用 #ifndef   A_H  #define   A_H  ...//Here   is   code.  #endif 效果类似. 包含pragma once语句的文件只会被编译一次. ...

  10. C之 #pragma(二十二)

    我们今天来介绍下 C 语言中的 #pragma,#pragma 用于指示编译器完成一些特定的动作.#pragma 所定义的很多指示字是编译器特有的,在不同的编译器间是不可移植的. 预处理期将忽略它不认 ...

最新文章

  1. VS2015自定义类模板的方法
  2. mysql_fetch_row ()出现段错误_207国道邵阳县段道路指示牌上出现错误,百姓疑惑”唐田市“在哪里...
  3. 当前超级计算机的应用方兴未艾,四川省若尔盖县高三下学期语文模拟卷(五)
  4. python内置方法怎么使用_python中的常用内置方法
  5. RLException: XXX is neither a launch file in package XXX nor is XXX a launch file name问题解决
  6. matlab2c使用c++实现matlab函数系列教程-ceil函数
  7. js 判断多个一样的name
  8. ubuntux学习日记
  9. 数据--第35课 - 创建二叉树
  10. 2.11.PHP7.1 狐教程-【PHP 数组】
  11. DS_Store文件泄漏
  12. dsp31段最佳调音图_dsp调音技巧_dsp调音最佳图
  13. 使用xsd文件验证xml
  14. python3爬虫的简单使用-附百度贴吧等大量实例代码
  15. Stc8A Air720D联调,问题(已解决)
  16. 【随笔记】做事要讲究轻重缓急,一定要把握主次轻重。
  17. C++:应用有限差分法求解随时间变化 平流方程 ut = - c * ux 在一个空间维度上,与 恒定速度,使用FTCS方法,正向时间差, 居中空间差(附完整源码)
  18. 企业微信+python实现监控服务器状态和实时查询数据
  19. 戴尔台式计算机怎么安装的,戴尔台式机重装系统教程
  20. 大人,时代变了——手游抽卡异军突起

热门文章

  1. kafka通过脚本一次启动集群
  2. 互联网晚报 | 9月3日 星期五 | vivo正式公布自研芯片V1;天猫公布今年双11节奏;网易云音乐去除歌曲“独家”标志...
  3. 【程序设计】前置检测循环和后置检测循环
  4. 【离散数学】命题逻辑符号化例题
  5. 【Java】二十一点小游戏
  6. 【数据结构与算法】实验 模拟FIFO网络打印机
  7. class类初始化之后调用赋值问题记录
  8. Java:双向链表反转实现
  9. Cacti0.8.8a监控mysql
  10. 重构随笔——重构的原则