| #define | |
| #undef | |
| #error | |
| #import | |
| #elif | |
| #if | |
| #include | |
| #using | |
| #else | |
| #ifdef | |
| #line | |
| #endif | |
| #ifndef | |
| #pragma |
コンパイラに対し、識別子をトークン文字列に置き換えるように指示するマクロを作成できます。
#define identifier token-stringopt
#define identifier ( identifieropt, ..., identifieropt ) token-stringopt#define ディレクティブ (C/C++) | MSDN
識別子を定義するだけならば、token-stringoptを省略します。Visual Studioではこれを、プロジェクトのプロパティの[C/C++] → [プリプロセッサ] → [プリプロセッサの定義]で設定できます。
コンパイルの前に、定めたパターンで文字を置換できます。
たとえば、
#define AAA 10
のようにマクロが定義されていると、
i = AAA
の記述はプリプロセッサにより、
i = 10
として展開されます。また引数を取るように
#define BBB(x,y) x<y
と定義されていると、
BBB(10,20)
の記述は、
10<20
として展開されます。
定義されている識別子を削除できます。
#undef identifier#undef ディレクティブ (C/C++) | MSDN
#if constant-expression
//
#elif constant-expression
//
#else
//
#endif
#if、#elif、#else、および #endif ディレクティブ (C/C++) | MSDN
constant-expressionには次の式を指定できます。
defined( identifier ) (かっこは省略可能)#ifdefは#if definedの短縮形であり、
#ifdef identifier
と記述することは、
#if defined identifier
と同義です。これは互換性のために用意されているディレクティブであり、必要がなければ#if definedとすべきです。#ifdef および #ifndef ディレクティブ (C/C++) | MSDN
たとえばVisual C++のビルド オプションで/D "_DEBUG"と指定されているとき、このディレクティブにより次のように分岐できます。
#ifdef _DEBUG
// デバッグ ビルド
#else
// リリース ビルド
#endif
#ifndef identifier
は、
#if !defined identifier
と同義です。
コンピュータまたはOS固有の機能を指定できます。
#pragma token-string( parameters )Pragma Directives and the __Pragma Keyword | Microsoft Learn
Microsoft CとC++コンパイラでは、token-stringに下表の文字列を指定できます。
| alloc_text | auto_inline | bss_seg |
| check_stack | code_seg | comment |
| component | conform ※1 | const_seg |
| data_seg | deprecated | detect_mismatch |
| fenv_access | float_control | fp_contract |
| function | hdrstop | include_alias |
| init_seg ※1 | inline_depth | inline_recursion |
| intrinsic | loop ※1 | make_public |
| managed | message | |
| omp | once | |
| optimize | pack | pointers_to_members ※1 |
| pop_macro | push_macro | region, endregion |
| runtime_checks | section | setlocale |
| strict_gs_check | unmanaged | vtordisp ※1 |
| warning |
オブジェクト ファイルまたは実行ファイルに、コメント記録を配置できます。
#pragma comment( comment-type [,"commentstring"] )Syntax - comment (C/C++) | Microsoft Learn
comment-typeには、下表の事前定義された識別子を指定します。
| 識別子 | 配置される情報 |
|---|---|
| compiler | コンパイラの名前とバージョン番号 |
| exestr | 文字列 |
| lib | ライブラリの検索 |
| linker | リンカーオプション |
| user | 文字列 |
たとえば次のように記述すると、リンク時にリンカーがsample.libを探すようになります。
#pragma comment( lib, "sample" )
これはVisual C++では、リンカーの設定で[追加の依存ファイル]を指定するのと同じです。
警告メッセージを制御できます。
// 警告メッセージの4507と4034を無効にする (Disable warning messages 4507 and 4034.) #pragma warning( disable : 4507 34 ) // 警告メッセージの4507の警告のふるまいを、既定に戻す。既定で無効ならば、有効にする #pragma warning( default : 4507) // 警告の4385を、一度だけ表示する (Issue warning 4385 only once.) #pragma warning( once : 4385 ) // 警告の4164を、エラーとして報告する (Report warning 4164 as an error.) #pragma warning( error : 164 )warning | MSDN
#import "filename" [ attributes ]The #import Directive | MSDN
attributesには、#import の属性 (C++)を指定できます。
| 演算子 | 動作 |
|---|---|
| 文字列化演算子 (#) | 対応する実引数が、二重引用符で囲まれる |
| 文字定数化演算子 (#@) | 対応する引数が単一引用符で囲まれ、文字として処理される (Microsoft 固有の仕様) |
| トークン連結演算子 (##) | 実引数として使用されるトークンが、他のトークンを形成するために連結されることを許可する |
| defined演算子 | 特定のマクロ ディレクティブで、複合式の記述を簡略化する |