CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jul 2008
    Posts
    42

    Question Facing problems with #ifdef directives in VS 2008

    Hello All,

    1. recently i have upraded my application from VS 2005 to VS 2008.

    2. I have a preprocessor directives defined in ProjectProperties | C/C++ | PreProcessor Definitions.

    3. I used this PreProcessor definition in a cpp file via #ifdef.

    /CODE
    #ifdef RIV
    ....
    ...
    #endif
    CODE/

    4. Observation is that, when i build by application in VS 2008 , code put in the #ifdef block is not getting executed at runtime. Whereas there was no problem with this piece of code when i build my application in VS 2005.

    5. pl. advice to resolve this issue.

  2. #2
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: Facing problems with #ifdef directives in VS 2008

    Hi,

    where did you define the "RIV"?
    Is there a
    Code:
    #define RIV
    statement anywhere in your code or is this defined elsewhere?

    Maybe it was predefined in your project's settings?

  3. #3
    Join Date
    Jul 2008
    Posts
    42

    Re: Facing problems with #ifdef directives in VS 2008

    please refer to #2 of my query. i have defined RIV in ProjectProperties.

  4. #4
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Facing problems with #ifdef directives in VS 2008

    Hi,

    Well, it *should* work. Just double-checked on VS2008 here.

    Adding this:
    Code:
     
    #ifdef RIV
    fall over
    #endif
    is seen by the compiler if I add RIV to the preprocessor definitions in the project properties.

    I suggest a clean and rebuild, then debug it by adding a #define RIV to your code to see what happens.

  5. #5
    Join Date
    Dec 2009
    Posts
    5

    Re: Facing problems with #ifdef directives in VS 2008

    hi alanjhd08,

    We too tried the option

    [CODE]

    #define RIV

    #ifdef RIV
    ...
    #endif

    [CODE]

    and it worked. But it our case is that this PreProcessor directive needs to be put via ProjectProperties. Because, this file is common for two projects, and one project needs a segment of code specific to it; hence the {#ifdef RIV ... #endif} block.

  6. #6
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Facing problems with #ifdef directives in VS 2008

    Hi, so you confirmed the problem is only when defining it via the Preprocessor.

    When you go into project properties, and look at C++ "Command Line" for the configuration you are building, do you see /D "RIV" under All options: ?

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Facing problems with #ifdef directives in VS 2008

    maybe you added the RIV define only in the release build settings and not in debug (or vice versa) ?

  8. #8
    Join Date
    Dec 2009
    Posts
    5

    Re: Facing problems with #ifdef directives in VS 2008

    1. RIV is defined in both the modes.. release & debug.
    2. /D "RIV" is shown in properties-> C/C++-> "Command Line".

    Still #ifdef RIV block is not getting executed.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Facing problems with #ifdef directives in VS 2008

    Quote Originally Posted by pmohod View Post
    1. RIV is defined in both the modes.. release & debug.
    2. /D "RIV" is shown in properties-> C/C++-> "Command Line".

    Still #ifdef RIV block is not getting executed.
    So we have these scenarios:

    1) Somewhere, some file has an #undef RIV before that block of code is executed.

    2) You don't have /D "RIV" in the project settings or your compiling something that isn't using those settings.

    3) You're observations are incorrect, and what is defined in #ifdef RIV is being executed.

    4) The Visual Studio IDE has a bug, and the make program isn't picking up the project settings.

    It isn't anything else. Let's look at 3).
    Code:
    #ifdef RIV
    #pragma message ("Hello -- RIV is defined OK!")
    #endif
    Now, if you compile your code, the compiler will output the message
    Code:
    Hello -- RIV is defined OK!
    in the compiler Output Window if RIV is really defined. If you do see it, then your observations are incorrect, and RIV is being defined. If you don't see it, then investigate the other possibilities.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Dec 2009
    Posts
    5

    Re: Facing problems with #ifdef directives in VS 2008

    One more clarification regarding our query

    1. If we add the preprocessor in the same project settings which creates the .exe, then it works
    2. However, we want to add the preprocessor in a library project settings and use it into the project that creates the application .exe

    Simplifying, suppose there are two projects - A and A_Lib
    Project A - creates the application A.exe ----> adding the preprocessor in this project settings works fine
    Project A_Lib - creates the library A_Lib.lib.

    Now, if we add the preprocessor in Project A_Lib settings, and use it in Project A, then it does not execute the #ifdef block.

    We have linked A_Lib with Project A (Properties --> Linker --> Input --> Additional Dependencies)

    This is what we are trying to overcome.

  11. #11
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Facing problems with #ifdef directives in VS 2008

    Hi,

    I'm not sure I understand the setup, can you confirm:

    The same arrangement worked fine under VS2005, only change is move to 2008.

    The code which includes #ifdef RIV is in the project A_lib, and RIV is defined as a preprocessor directives in ProjectProperties | C/C++ | PreProcessor Definitions for that project.

    You build the project A_lib with those settings, before linking it from project A.

    Is that right? Or maybe the project A_lib isn't getting rebuilt for some reason, but you expect it to because it is defined as an additional dependency?

  12. #12
    Join Date
    Dec 2009
    Posts
    5

    Re: Facing problems with #ifdef directives in VS 2008

    Hi alanjhd08,

    You interpreted all the points correctly except point #2. Let me clarify.

    1. "The same arrangement worked fine under VS2005, only change is move to 2008." ----> Correct

    2. The code which includes #ifdef RIV is in the project A (which creates .exe).

    RIV is defined as a preprocessor directives in ProjectProperties | C/C++ | PreProcessor Definitions for project A_lib.

    3. "You build the project A_lib with those settings, before linking it from project A." ----> Correct

    .... We rebuilt project A_Lib explicitly before building Project A.

    Can you guide us where we are going wrong?

  13. #13
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Facing problems with #ifdef directives in VS 2008

    Hi,

    Sorry, I can't think of anything.

    I can't see how defining RIV for the library, but not the project that includes the code, would ever have worked, so looks like I'm misunderstanding something.

    Alan

  14. #14
    Join Date
    Dec 2009
    Posts
    5

    Smile Re: Facing problems with #ifdef directives in VS 2008

    Hi All,

    Our issue has been sorted internally. Turned out we had added some libs which were not required in the settings, and they were over-riding the RIV preprocessor. Removing those unwanted lib depencies sorted the problem.

    Thank you all for your efforts and help, really appreciated :-):-)

  15. #15
    Join Date
    Jul 2008
    Posts
    42

    Resolved Re: Facing problems with #ifdef directives in VS 2008

    Many thanks to the codeguru community for interacting with us and giving us relevant directions to analyse the situation.

    #1 of Paul McKenzie April 7th, 2010 02:42 PM gave some thoughts to us in investigating which might be nullifying the definition of RIV PreProcessor.

    If I continue to borrow the terms A.Exe, A_Lib.lib, we have put A_Lib.lib as the first library in Linker | Input | Additional Dependencies. This made "#ifdef RIV" block of code work.

    Then we started Lowering the position of this A_Lib.lib until "#indef RIV" failed, thus identifying which lib in the list of dependencies is nullifying the RIV definition.

    It turned out that addition of this B.lib in the list of additional dependencies is a mistake on the part of the developer. We rectified it, and thereafter things went well.

    i give these details so that if someone somewhere gets into this kind of issues, and happens to read this post, he may get a skill to do the trouble shooting.

    Thanks once again to the community.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured