Cprogramming 简明教程

==

What is

preprocessor 指令 #pragma 用于在 C/C++ 语言中向编译器提供附加信息。编译器使用该信息提供一些特殊功能。

The preprocessor directive #pragma is used to provide additional information to the compiler in C/C++ language. This is used by the compiler to provide some special features.

请注意 pragmas are compiler dependent 。并非所有 pragma 指令都受所有编译器支持。

Note that pragmas are compiler dependent. Not all the pragma directives are supported by all the compilers.

Syntax

以下是 C/C++ 语言中使用 #pragma 指令的 syntax

Here is the syntax of using a #pragma directive in C/C++ language −

#pragma token_name

Types of Pragma Directives in C

下表列出了 C/C++ 语言中一些 #pragma 指令,

The table of some of #pragma directives in C/C++ language is given as follows,

Directive

Description

#pragma startup

Before the execution of main(), the function specified in pragma is needed to run.

#pragma exit

Before the end of program, the function specified in pragma is needed to run.

#pragma warn

Used to hide the warning messages.

#pragma GCC dependency

Checks the dates of current and other file. If other file is recent, it shows a warning message.

#pragma GCC system_header

It treats the code of current file as if it came from system header.

#pragma GCC poison

Used to block an identifier from the program.

#pragma once

Compiler loads the header file only once.

===

这些 pragma 指令在 main() function 的之前和之后执行。并非所有编译器都支持这些指令。

These pragma directives are executed before and after the main() function. Not all the compilers support these directives.

Example

以下代码演示了如何使用 pragma startup 和 exit 指令−

The following code demonstrates how you can use the pragma startup and exit directives −

#include <stdio.h>

int display();

#pragma startup display
#pragma exit display

int main(){

   printf("\nI am in main function");
   return 0;
}

int display() {
   printf("\nI am in display function");
   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

I am in main function

===

#pragma warn 指令用于隐藏或显示编译时显示的警告信息。

The #pragma warn directive is used to hide or display the warning messages which are displayed during compilation.

warn pragma 按照以下 syntax 使用−

The warn pragma is used as per the following syntax

#pragma warn +xxx (To show the warning)
#pragma warn -xxx (To hide the warning)
#pragma warn .xxx (To toggle between hide and show)

要使用的三个字符代码是 rvl (返回值)、 par (使用或不使用参数)和 rch (如果代码不可达)。

The three character codes to be used are rvl (return value), par (parameter used or not), and rch (if the code is unreachable).

如果字符代码之前带有“+”,表示显示警告;如果之前带有“–”,表示向编译器提出隐藏警告的指示;如果之前带有句点 (.),则为对编译器提出的在隐藏和显示警告之间切换的指令。

If any character code is prefixed by "+", it indicates to show the warning; prefixed by "–" means indication to the compiler to hide warnings, and prefix by dot (.) is an instruction to the compiler to toggle between hide and display warnings.

Example

以下示例显示了如何在 C 程序中使用 warn pragma −

The following example shows how you can use the warn pragma in a C program −

#include <stdio.h>

#pragma warn -rvl          /* return value */
#pragma warn +par	   /* parameter never used */
#pragma warn –rch	   /* unreachable code */

int square(int x){
   printf("Hello World");
}

int main(){

   square(10);
   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Hello World

===

GCC 编译器从程序中完全删除 identifier 。如果要阻止标识符,则可以使用 #pragma GCC poison 指令。其语法如下 −

The GCC compiler removes an identifier completely from the program. If we want to block an identifier, then we can use the #pragma GCC poison directive. Its syntax is as follows −

#pragma GCC poison identifier

Example

在此示例中,我们将使用 GCC poison pragma 来阻止 printf() function

In this example, we will use the GCC poison pragma to block the printf() function

#include <stdio.h>

#pragma GCC poison printf

int main(){

   int a = 10;
   if (a == 10) {
      printf("Hello World");
   }
   else
      printf("TutorialsPoint");

   return 0;
}

当您尝试编译此代码时,将显示以下错误 −

When you try to compile this code, it will show the following error −

error: attempt to use poisoned "printf"

===

此 pragma 允许您检查当前文件和另一个文件的相对日期。如果另一个文件比当前文件新,则会发出警告。

This pragma allows you to check the relative dates of the current file and another file. If the other file is more recent than the current file, a warning is issued.

Example

请看以下示例:

Take a look at the following example −

#include <stdio.h>

#pragma GCC dependency "depends.c"

int main(){

   printf("Hello, World!");
   return 0;
}

上面的源代码依赖于 depends.c。如果其编译时间戳较新,则会发出以下 warning

The above source code is depending on depends.c. If its compilation timestamp is more recent, then the following warning is issued −

warning: current file is older than depends.c

===

按照惯例,系统头文件在 #include directive 前面放置尖括号,而非系统头文件则用引号括起。如果您希望编译器将后者视为系统头文件,请使用此 pragma。

As a convention, system header files are placed angular brackets in front of the #include directive, whereas the non-system header files are in quotation marks. If you want the compiler to treat the latter as the system header, use this pragma.

Syntax

#pragma GCC system_header

library.h

我们在当前目录中定义了一个“library.h”头文件。

We define a "library.h" header file in the current directory.

#ifndef LIBRARY_H
#define LIBRARY_H

#pragma GCC system_header

void myFunction();

#endif

Example

要让编译器将“library.h”视为系统头文件,请使用 #pragma GCC system_header。

To ask the compiler to treat "library.h" as a system header, use the #pragma GCC system_header.

#include <stdio.h>
#include "library.h"

int main(){

   myFunction();	// Using a function from the library.h

   printf("Hello, World!\n");
   return 0;
}

===

#pragma once 指令会导致头文件仅包含一次,即使程序员多次包含它。

The #pragma once directive causes the header file to be included only once, even if the programmer includes it multiple times.

将“myheader.h”文件另存为 −

Save the "myheader.h" file as −

myheader.h

#pragma once
void myFunction();

Example

在另一个代码 (main.c) 中,按如下方式调用 myFunction() −

In another code (main.c), call myFunction() as follows −

#include <stdio.h>
#include "myheader.h"

int main(){

   myFunction();
   printf("Hello, World!\n");
   return 0;
}