編譯指示 #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. windows 编程—— 学习指导

    这里有一份很好的资源,被制作成chm文件的<Windows 程序设计>,包含了中文版和英文版,还有全书源代码,虽然不知道是谁出版的,但是感觉对Windows编程新手来说还是很不错的.关键还 ...

  2. WORLD PROBLEMS

    World中遇到到一些问题     1. 由于文件许可权错误,word无法完成保存操作 我的选择另存为有效,没有安装卡巴斯基8.0(Kaspersky Internet Security 8.0.0. ...

  3. CF380C Sereja and Brackets [想法+线段树]

    题意: 给出一串括号 给出一些询问,问某个区间[l,r]内的能合法匹配的括号数有多少个 分析: 我们能够实现处理两个数组 sum[i] 1....i中已经能匹配的右括号的数目 left[i] 1... ...

  4. ViewPager 详解(四)----自主实现滑动指示条

    前言:前面我们用了三篇的时间讲述了有关ViewPager的基础知识,到这篇就要进入点实际的了.在第三篇<ViewPager 详解(三)---PagerTabStrip与PagerTitleStr ...

  5. Task的运行过程分析

    Task的运行过程分析 Task的运行通过Worker启动时生成的Executor实例进行, caseRegisteredExecutor(sparkProperties)=> logInfo( ...

  6. Two-Phase-Commit for Distributed In-Memory Caches--reference

    Part I reference from:http://gridgain.blogspot.kr/2014/09/two-phase-commit-for-distributed-in.html 2 ...

  7. Collections.unmodifiableList()的使用与场景

    在<重构——改善既有代码的设计>一书中,有一种重构手法叫Encapsulate Collection(封装集群),为了演示该重构手法,我写了四个类,通过对比重构前后的代码,加深对这一重构手 ...

  8. 洛谷 P1412 经营与开发

    /* 粘一下开始写的暴力吧 虽然没啥价值 */ #include<iostream> #include<cstdio> #include<cstring> #inc ...

  9. 服务器端调用Word组件读取Word权限、未将对象引用到对象实例终极解决方案

    最近因为业务需要,需要在服务器上调用Word组件,结果遇到各种问题,比如检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败 ...

  10. WCF 接收我服务的 HTTP 响应时发生错误

    错误内容: System.ServiceModel.CommunicationException: 接收对 https://xx.com/xx.svc的 HTTP 响应时发生错误.这可能是由于服务终结 ...