Directives in C++

Type DirectivesExplanations
#defineCreates a macro, which is the association of an identifier or parameterized identifier with a token string.
#elifA directive  used for conditional compilation
#elseA directive  used for conditional compilation
#endifA directive  used for conditional compilation
#errorDirective emits a user-specified error message at compile time. Then it terminates the compilation.
#ifA directive  used for conditional compilation
#ifdefUsed 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.
#importUsed to incorporate information from a type library. The content of the type library is converted into C++ classes, mostly describing the COM interfaces.
#includeTells the preprocessor to include the contents of a specified file at the point where the directive appears.
#lineTells the preprocessor to set the compiler’s reported values for the line number and filename to a given line number and filename.
#warningEmits a user-specified warning message at compile time. It doesn’t stop compilation. This directive is available starting in C23 and C++23.
#pragmaThe method specified by the C standard for providing additional information to the compiler, beyond what is conveyed in the language itself.
#undefRemoves (undefines) a name previously created with #define.
#usingImports 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

https://www.youtube.com/watch?v=VltPggpVJN0