[RESOLVED] Using Macro value in VC++ header file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] Using Macro value in VC++ header file

  1. #1
    Join Date
    Jun 2017
    Posts
    5

    [RESOLVED] Using Macro value in VC++ header file

    Hi i have 2 header files in my project.
    Compile.h has the following contents

    #define Testcase 1

    Config.h has the folowing contents
    #define iTimer 4
    #define iCounter 5

    I want to use the macro Testcase in my Config.h file like below

    #ifdef Testcase
    #undef iTimer
    #define iTimer 5
    #undef iCounter
    #define iCounter 4
    #endif

    Will this change work? I just want to swap the values of iTimer and iCounter if the macro Testcase is defined in my code. For some tests i comment the macro in the config.h file. Please help me in this.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,706

    Re: Using Macro value in VC++ header file

    Yes, but don't forget that macros are local to the compilation unit in which they are used. So if the #ifdef is in a unit and not the header, you'll need it in each unit required.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  3. #3
    Join Date
    Jun 2017
    Posts
    5

    Re: Using Macro value in VC++ header file

    Quote Originally Posted by 2kaud View Post
    Yes, but don't forget that macros are local to the compilation unit in which they are used. So if the #ifdef is in a unit and not the header, you'll need it in each unit required.
    Hi
    I am still confused with your answer.. iTimer and iCounter are used in many of my .c source files. DO i need to make the change there?
    Should i include a header file in the other header file?
    Please advice me. I am new to this.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,706

    Re: Using Macro value in VC++ header file

    In config.h, if you have
    Code:
    #define testcase 1
    
    #define iTimer 4
    #define iCounter 5
    
    #ifdef Testcase	
    #undef iTimer
    #define iTimer 5
    #undef iCounter
    #define iCounter 4
    #endif
    Then that's fine as long as you #inciude config.h in all the required .c source files.

    If however in config.h you just have
    Code:
    #define testcase 1
    
    #define iTimer 4
    #define iCounter 5
    then either you need to add the #ifdef... code to config.h as above - or you need to add this to every .c source file that requires it.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  5. #5
    Join Date
    Jun 2017
    Posts
    5

    Re: Using Macro value in VC++ header file

    Quote Originally Posted by 2kaud View Post
    In config.h, if you have
    Code:
    #define testcase 1
    
    #define iTimer 4
    #define iCounter 5
    
    #ifdef Testcase	
    #undef iTimer
    #define iTimer 5
    #undef iCounter
    #define iCounter 4
    #endif
    Then that's fine as long as you #inciude config.h in all the required .c source files.

    If however in config.h you just have
    Code:
    #define testcase 1
    
    #define iTimer 4
    #define iCounter 5
    then either you need to add the #ifdef... code to config.h as above - or you need to add this to every .c source file that requires it.
    Hi
    But #define Testcase 1 is not in config.h.
    It is in compile.h file and i cannot define it in config.h..i just have to add a small piece of code in the config.h file to swap the values of itimer and iCounter only if the Testcase is defined in config.h.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,706

    Re: Using Macro value in VC++ header file

    Doh! You are right. config.h needs to include compile.h (or the .c files need to #include compile.h before #include config.h).

    compile.h
    Code:
    #define Testcase 1
    config.h
    Code:
    #include "compile.h"
    
    #define iTimer 4
    #define iCounter 5
    
    #ifdef Testcase	
    #undef iTimer
    #define iTimer 5
    #undef iCounter
    #define iCounter 4
    #endif
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  7. #7
    Join Date
    Jun 2017
    Posts
    5

    Re: Using Macro value in VC++ header file

    Thanks alot. I was so confused about this. You really solved my problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)