Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. control #define directive

control #define directive

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    p3im4n
    wrote on last edited by
    #1

    hi i want to control parts of my code with #ifdef directive,so I need to control a #define directive before! something like this:

    if (condition)
    #define active

    and in other files I will check whether "active" is defined or not! ok,I can put :

    #define active

    in header file then use it in others cpp files but I cant use if statement in header! how can i do that? cheers, peiman

    J L 2 Replies Last reply
    0
    • P p3im4n

      hi i want to control parts of my code with #ifdef directive,so I need to control a #define directive before! something like this:

      if (condition)
      #define active

      and in other files I will check whether "active" is defined or not! ok,I can put :

      #define active

      in header file then use it in others cpp files but I cant use if statement in header! how can i do that? cheers, peiman

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      Please explain your problem using an example. It is (at least for me) unclear. Common practice is to define a value somewhere (inside a header file or even passing it to the compiler via command line; optionally by project settings). So assuming you use ACTIVE as boolean condition (defined or not defined):

      // General header file
      // Comment this to undefine
      #define ACTIVE

      // Any other file that has included above header file
      #ifdef ACTION
      // Do something
      #endif

      #ifndef ACTION
      // Do something else
      #endif

      But this won't work if there is a code condition like

      if (condition)

      Then you have to use similar code conditions in all other places. Note also that I have used all upper case for the defined value because this is common practice. To know about conditional inclusions using the preprocessor see Preprocessor directives - C++ Tutorials[^].

      1 Reply Last reply
      0
      • P p3im4n

        hi i want to control parts of my code with #ifdef directive,so I need to control a #define directive before! something like this:

        if (condition)
        #define active

        and in other files I will check whether "active" is defined or not! ok,I can put :

        #define active

        in header file then use it in others cpp files but I cant use if statement in header! how can i do that? cheers, peiman

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        p3im4n wrote:

        but I cant use if statement in header!

        Why not? A header is just a text file that gets included into, and compiled as part of, some source code. It is quite common to do something like:

        // in header file
        #if defined(foo)
        // some code to be generated
        #endif

        // in cpp file
        #define foo
        #include

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups