| Type Directives | Explanations |
| #define | Creates a macro, which is the association of an identifier or parameterized identifier with a token string. |
| #elif | A directive used for conditional compilation |
| #else | A directive used for conditional compilation |
| #endif | A directive used for conditional compilation |
| #error | Directive emits a user-specified error message at compile time. Then it terminates the compilation. |
| #if | A directive used for conditional compilation |
| #ifdef | Used to include a section of code if a certain macro is defined by #define. |
#ifndef | Used to include a section of code if a certain macro is not defined by #define. |
| #import | Used to incorporate information from a type library. The content of the type library is converted into C++ classes, mostly describing the COM interfaces. |
| #include | Tells the preprocessor to include the contents of a specified file at the point where the directive appears. |
| #line | Tells the preprocessor to set the compiler’s reported values for the line number and filename to a given line number and filename. |
| #warning | Emits a user-specified warning message at compile time. It doesn’t stop compilation. This directive is available starting in C23 and C++23. |
| #pragma | The method specified by the C standard for providing additional information to the compiler, beyond what is conveyed in the language itself. |
| #undef | Removes (undefines) a name previously created with #define. |
| #using | Imports metadata into a program compiled with /clr. |
Examples of Directives:
#define
#define ONE 1
#define multiplication(f1,f2) (f1*f2)
#elif, #if, #else, #endif
#if defined(CREDIT)
credit();
#elif defined(DEBIT)
debit();
#else
printerror();
#endif
#error
#if !defined(__cplusplus)
#error C++ compiler required.
#endif
#ifdef, #endif
// ifdef_ifndef.CPP
// compile with: /Dtest /c
#ifndef test
#define final
#endif
#import
#import "progid:my.prog.id.1.5"
#include
#include <stdio.h>
#line
// line_directive.cpp
// Compile by using: cl /W4 /EHsc line_directive.cpp
#include <stdio.h>
int main()
{
printf( "This code is on line %d, in file %s\n", __LINE__, __FILE__ );
#line 10
printf( "This code is on line %d, in file %s\n", __LINE__, __FILE__ );
#line 20 "hello.cpp"
printf( "This code is on line %d, in file %s\n", __LINE__, __FILE__ );
printf( "This code is on line %d, in file %s\n", __LINE__, __FILE__ );
}
This would result:
This code is on line 7, in file line_directive.cpp
This code is on line 10, in file line_directive.cpp
This code is on line 20, in file hello.cpp
This code is on line 21, in file hello.cpp
#warning
#if defined(_LEGACY_FEATURE_FLAG)
#warning "_LEGACY_FEATURE is deprecated and should not be used."
#endif
#pragma
// some_file.cpp - packing is 8
// ...
#pragma pack(push, 1) - packing is now 1
// ...
#pragma pack(pop) - packing is 8 again
// ...
#undef
#define WIDTH 80
#define ADD( X, Y ) ((X) + (Y))
.
.
.
#undef WIDTH
#undef ADD
#using
// using_assembly_B.cpp
// compile with: /clr /LD
#using "using_assembly_A.dll"
public ref class B {
public:
void Test(A a) {}
void Test() {}w
};
Refrences
https://learn.microsoft.com/en-us/cpp/preprocessor/preprocessor-directives?view=msvc-170