編譯指示 #pragma 是用來告知編譯器某些特殊指示,例如不要輸出錯誤訊息,抑制警告訊息,或者加上記憶體漏洞檢查機制等。這些指示通常不是標準的 C 語言所具備的,而是各家編譯器廠商或開發者所制定的,以便讓編譯器可以具有某些特殊的選項。

舉例而言,#pragma STDC 就可以用來要求編譯器採用標準 C 的語法進行編譯,只要看到有任何不符合標準 C 的語法,編譯器就會輸出錯誤。

#Pragma message

#ifdef _X86
#Pragma message("_X86 defined") // 在編譯時輸出 _X86 defined
#endif

#Pragma warning

#pragma warning( once:37 43; disable:32; error:17) // 37,43 只警告一次,不警告 32 號資訊,17 號警告視為錯誤

#pragma warning( push ) // 保存目前警告狀態
#pragma warning( once:37 43)
#pragma warning( disable:32 )
#pragma warning( error:17 )
…….
#pragma warning( pop ) // 恢復先前的警告狀態

#pragma warn -100 // Turn off the warning message for warning #100
int insert_record(REC *r) {

}
#pragma warn +100 // Turn the warning message for warning #100 back on

#Pragma once

#pragma once // 保證引用檔 (*.h) 只會被引用一次,如此就不需要用「引入防護」了。

Pragma code_seg

#Pragma code_seg(["section-name"][,"section-class"])

#pragma code_seg("INIT") // 設定存放於 INIT 區段,開發驅動程式時會用到
extern"C"
void DriverEntry(…) { … }

#pragma hdrstop

#pragma hdrstop // 表示引用檔編繹到此為止,以加快編譯速度。

#pragma startup

#pragma startup <func> <priority>
#pragma exit <func> <priority>

void india();
void usa() ;
#pragma startup india 105
#pragma startup usa
#pragma exit usa
#pragma exit india 105
void main() { printf("\nI am in main"); getch(); }
void india() { printf("\nI am in india"); getch(); }
void usa() { printf("\nI am in usa"); getch(); }

執行結果

I am in usa
I am in India
I am in main
I am in India
I am in usa

#pragma package(…)

#pragma package(smart_init) // 使用某套編譯指引 (在 BCB 中,根據優先級的大小先後編譯)

#pragma resource "…"

#pragma resource "*.dfm" // 把*.dfm 資源檔加入專案。

#pragma loop_opt(…)

每個編譯程式可以用#pragma指令激活或終止該編譯程式支援的一些編譯功能。例如,對迴圈優化功能:

#pragma loop_opt(on) // 啟動迴圈最佳化
#pragma loop_opt(off) // 停止迴圈最佳化

#pragma asm

#pragma asm // 代表後面寫的是組合語言 (Microsoft)

#pragma small

#pragma small // 使用小記憶體模式 (Microsoft X86)

#pragma registerbank(..)

#pragma registerbank(0) // 使用 8031 處理器中的 bank0 (Keil C)

#pragma code

#pragma code // 表示唯讀資料應儘可能放在 ROM 裡以節省 RAM (Keil C)

參考文獻

    1. pragma 預處理指令 — http://topalan.pixnet.net/blog/post/22334686

C 語言中的編譯指示 (Pragma)的更多相关文章

  1. 查看LINQ Expression編譯後的SQL語法(转)

    在用了LINQ語法之後的一個月,我幾乎把SQL語法全部拋到腦後了,不過 LINQ好用歸好用,但是實際上操作資料庫的還是SQL語法,如果不知道LINQ語法 編譯過後產生怎樣的SQL語法,一不小心效能就會 ...

  2. Centos7編譯安裝LAMP平臺

    什麽是LAMP? 拆開看 L 就是Linux系統 A是Apache的縮寫 M.P則是MySQL和PHP的简写. 其实就是把Apache, MySQL以及PHP安装在Linux系统上,组成一个环境来运行 ...

  3. CentOS 7 編譯qBittorrent Web UI安裝指南

    前言 以下是安装qBittorrent教学. 适用于CentOS7或更新版本 适用于qBittorrent4.1.5或更新版本 安裝須知 qBittorrent基于libtorrent,所以必须先安装 ...

  4. windows 安裝 gcc 編譯器 - MinGW

    MinGW 介紹 MinGW 全稱 Minimalist GNU For Windows,是個精簡的Windows平台C/C++.ADA及Fortran編譯器,相比Cygwin而言,體積要小很多,使用 ...

  5. MAC下反編譯安卓APK

    所需工具: 1.apktool   http://ibotpeaches.github.io/Apktool/  (配置說明)       https://bitbucket.org/iBotPeac ...

  6. 【转】編譯Ogre1.9 IOS Dependencies及Ogre Source步驟及相關注意事項…

    http://makedreamvsogre.blogspot.tw/2014/01/ios-dependenciesogre-source.html 可能有人會想問我為什麼要自行編譯IOS Depe ...

  7. llvm學習(二)————llvm編譯與環境構建

    本文由博主原创,转载请注明出处(保留此处和链接): IT人生(http://blog.csdn.net/robinblog/article/details/17339027) 在2011十月份的时候, ...

  8. make command explaination 編譯命令解釋

    Creating .config file make ARCH=arm CROSS_COMPILE=arm-none-eabi- stm32_defconfig 以上命令是 將變數 ARCH=arm, ...

  9. High-level structure of a simple compiler高級結構的簡單編譯器

    1.lexical analysis,which analyzes the character string presented to it and divides it up into tokens ...

随机推荐

  1. 解决Xcode7 iOS9苹果将原http协议改成了https协议问题

    在info.plist 加入key <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbi ...

  2. Markdown入门指南-指间阁

    宗旨 Markdown 的目标是实现「易读易写」. 可读性,无论如何,都是最重要的.一份使用 Markdown 格式撰写的文件应该可以直接以纯文本发布,并且看起来不会像是由许多标签或是格式指令所构成. ...

  3. 转载:c++内存泄露机制

    对于一个c/c++程序猿来说,内存泄漏是一个常见的也是令人头疼的问题.已经有很多技术被研究出来以应对这个问题,比方 Smart Pointer,Garbage Collection等.Smart Po ...

  4. 项目总结——深入浅出socket网络编程

    前言: 为什么会有如题的概念呢,我想对于没有主动听说过socket网络编程的人来说读到题目可能就已经蒙头了,为了很好的让大家进入场景,首先说一下一个需要用到这点东西的业务需求. 首先大家应该明确的是s ...

  5. [Angular 2] Dispatching Action with Payloads and type to Reducers

    While action types allow you tell your reducer what action it should take, the payload is the data t ...

  6. hdu1074 Doing Homework(状态压缩DP Y=Y)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. Linux LVM 扩展磁盘分区

    系统:centos 6.3--新建分区 fdisk -l /dev/sdc       # 查看分区 fdisk /dev/sdc          # 创建分区 :n                 ...

  8. codevs 2241 排序二叉树

    /* WTF 写了好久了 开始的时候题目读错了 建图建错了 搜索写的也不好 感觉会T 总之 第一次写的很low 贴一下吧 */ #include<iostream> #include< ...

  9. C#第一节课

    1,命名规范 A.如果声明一个变量,小写,如果有多个单词,后面首字母大写 如: string sString="aa"; int iNum=20; bool bMale=false ...

  10. hdu 2304

    题意: 插座插空问题 水题.....只要知道最后一个不需要插即可.... 直接贴代码.. AC代码: #include <iostream> using namespace std; in ...