Pragma directives specify machine-specific or operating system-specific compiler features. A line that begins withDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/MicrosoftDocs/cpp-docs/llms.txt
Use this file to discover all available pages before exploring further.
#pragma passes an instruction directly to the compiler — after the preprocessor has expanded any macros in the token string. If the compiler does not recognize a pragma, it issues a warning and continues compilation, which makes pragmas safe to use even in code that may be compiled by multiple compilers (unrecognized pragmas are simply ignored).
MSVC also supports two alternative pragma forms:
__pragma and _Pragma forms let you embed pragma directives inside macro definitions, which is impossible with #pragma because the # character would be interpreted as the stringizing operator.
#pragma once
#pragma once tells the compiler to include a header file only the first time it is encountered in a given translation unit. It is the modern, idiomatic alternative to the traditional #ifndef include guard.
Comparison with Include Guards
#pragma comment
#pragma comment inserts a record into the generated object file (.obj). The linker reads these records when combining object files, enabling you to embed linker instructions directly in source code.
Syntax
Comment Types
lib — Automatic Library Linking
lib — Automatic Library Linking
The most commonly used This technique lets you put library dependencies next to the code that uses them, so developers don’t need to separately configure project linker settings.
comment type. It instructs the linker to search for the named library, just as if you had specified it on the command line with /link libname.lib.linker — Embed Linker Options
linker — Embed Linker Options
Places a linker option directly into the object file. Only a subset of linker options can be specified this way.
compiler — Embed Compiler Version
compiler — Embed Compiler Version
Places the compiler name and version string into the object file (informational only; ignored by the linker):
user — Embed Arbitrary Comment
user — Embed Arbitrary Comment
Places a general text comment into the object file (ignored by the linker):
#pragma warning
#pragma warning enables fine-grained control over MSVC compiler warning messages. You can suppress, promote, or adjust the severity of individual warnings — or push and pop warning states to isolate changes to a specific code region.
Syntax
Warning Specifiers
| Specifier | Effect |
|---|---|
disable | Suppress the listed warning(s) entirely |
error | Treat the listed warning(s) as errors |
once | Issue the listed warning(s) at most one time |
default | Restore the listed warning(s) to their default level |
suppress | Disable the listed warning for the next single line, then restore |
1, 2, 3, 4 | Set the listed warning(s) to the given severity level |
Examples
Push and Pop — Scoped Warning State
The push/pop mechanism saves and restores the entire warning state, making it safe to change warnings for a block of code without affecting the rest of the file:Warning numbers in the range 4700–4999 are code-generation warnings. Changes to these warnings take effect at the end of the current function definition, not at the pragma’s location in the source. For these warnings, place the
#pragma warning directive before the function definition.#pragma optimize
#pragma optimize controls the optimization settings applied to individual functions. It must appear outside a function definition and takes effect at the first function defined after the pragma.
Syntax
Optimization Parameters
| Parameter | Optimization type |
|---|---|
g | Global optimizations (deprecated; use /Og) |
s | Favor short machine code sequences |
t | Favor fast machine code sequences |
y | Omit frame pointers on the program stack |
Examples
#pragma pack
#pragma pack controls the alignment (packing) of structure, union, and class members. By default, the compiler aligns members on boundaries suited to the target architecture (typically the smaller of the member size and the default packing size of 8 bytes). Reducing packing can save memory at the cost of potential unaligned access performance penalties.
Syntax
n: 1, 2, 4, 8, 16.
Example — Reducing Padding
Network Protocol Headers
#pragma pack is especially important when mapping C structures directly onto wire protocols or binary file formats:
#pragma section
#pragma section creates a named section in the output object file (.obj). You then use __declspec(allocate("section-name")) to place specific variables into that section.
Syntax
read, write, execute, shared, nopage, nocache, discard, remove.
Example
Once a section is defined with
#pragma section, it remains valid for the rest of the compilation unit. However, a variable is only placed in that section if it carries the __declspec(allocate("section-name")) attribute.#pragma intrinsic
#pragma intrinsic tells the compiler to treat calls to the listed functions as compiler intrinsics — inlining the function body with a few machine instructions rather than generating a real function call. Intrinsic forms exist for common library functions like memcpy, memset, strcmp, and mathematical functions.
Syntax
Example
abs, fabs, memcmp, memcpy, memset, strcat, strcmp, strcpy, strlen, _rotl, _rotr, _lrotl, _lrotr.
To revert to the normal library call, use #pragma function:
Using __pragma Inside Macros
Because #pragma cannot appear inside a macro definition (the # would be interpreted as the stringizing operator), MSVC provides __pragma as a keyword alternative and C99 provides _Pragma as a standard operator:
Complete Pragma Quick Reference
| Pragma | Purpose |
|---|---|
#pragma once | Prevent multiple inclusion of a header |
#pragma comment(lib, "name.lib") | Auto-link a library |
#pragma comment(linker, "option") | Embed a linker option |
#pragma warning(disable : N) | Suppress warning N |
#pragma warning(push/pop) | Save and restore warning state |
#pragma optimize("flags", on/off) | Control per-function optimization |
#pragma pack(n) | Set struct member alignment |
#pragma pack(push/pop) | Save and restore alignment state |
#pragma section("name", attrs) | Create a named object file section |
#pragma intrinsic(fn) | Use intrinsic form of a function |
#pragma function(fn) | Revert to library call for a function |
#pragma message("text") | Print a message during compilation |
#pragma hdrstop | Mark end of precompiled header includes |
#pragma data_seg("name") | Place initialized data in named section |
#pragma bss_seg("name") | Place uninitialized data in named section |
#pragma code_seg("name") | Place code in named section |
See Also
- Preprocessor Directives —
#include,#define,#if,#error,#line - Macros (C/C++) — Macro expansion,
#,##,__VA_ARGS__, predefined macros - C Language Preprocessor Overview
- MSVC Pragma Reference (all pragmas)