Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • ๐Ÿ›ฃ๏ธC Plus Plus: Docs
  • Build
    • ๐Ÿ”ฅBuild Process
    • ๐Ÿ”งConnect multiple C++ Files
  • Code
    • โฎ๏ธPre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • ๐Ÿช…Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • ๐ŸŽญCasting
    • ๐Ÿ”ƒOperator overloading
      • Available Operators
      • Examples of operator overloading
    • ๐Ÿ—บ๏ธNamespace
      • Namespace Example
      • Using directive
    • ๐Ÿ•ต๏ธHeader File
      • For C++ Classes
    • ๐Ÿ—๏ธStructure
      • Struct vs Class
      • Public vs Private inheritance
    • ๐ŸขClasses
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • ๐Ÿง Memory Introduction
    • โœจHeap and Stack
    • ๐ŸŽฏPointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • ๐Ÿ‘จโ€๐ŸญHelper function
    • ๐ŸกVector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • โ™ป๏ธUnion
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • ๐ŸงตThreading
      • std::thread
      • Detach a thread
  • Misc
    • ๐Ÿ—‚๏ธExecution Order
    • ๐Ÿง Print memory
Powered by GitBook
On this page
  1. Code
  2. Pre-Processors

#ifdef

These directives allow you to include or exclude code based on certain conditions

Previous#defineNext#pragma

Last updated 10 months ago

If we include a header file 2 times (maybe in multiple files), we will get error saying multiple declaration found.

To overcome this, we need to ignore multiple inclusion, we define a . which we check if ifdef and ignore inclusion if already incuded.

Here is an example:

#define DEBUG

#ifdef DEBUG
    std::cout << "Debug mode is on" << std::endl;
#endif

#ifndef NDEBUG
    // Code for debug builds
#else
    // Code for release builds
#endif

Or another example, we check system config, if system is unix we use cls and if it us linux we use clear and many more.

#ifdef _WIN32
    #define CLEAR_SCREEN "cls"
#else
    #define CLEAR_SCREEN "clear"
#endif

#if defined(__cplusplus) && __cplusplus >= 201703L
    // Use C++17 features
#elif defined(__cplusplus) && __cplusplus >= 201402L
    // Use C++14 features
#else
    #error "This program requires at least C++14"
#endif
โฎ๏ธ
macro