Thread: #ifdef error

  1. #1
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001

    #ifdef error

    Ran into a problem in MSVC 2008 today that I cannot explain.

    Take the following sample code:

    #pragma once
    #ifdef MYDLL_EXPORTS
      #define MYDLL_DECL __declspec(dllexport)
      #define MYDLL_DECL __declspec(dllimport)
    MYDLL_EXPORTS is defined in the project pre-processor settings.

    The problem I ran into is that even when MYDLL_EXPORTS was defined MSVC 2008 greyed out the first define and not the second. When I tested it and added #define MYDLL_EXPORTS prior to the #ifdef it then would not compile saying that MYDLL_EXPORTS was a duplicate define. After removing the #define now MSVS 2008 has greyed out the #ifdef line.

    What in the world? I have 9 other DLL projects using the same type of mechanism in the same solution and they all work. However for this one file only it is saying that the MYDLL_EXPORTS is not defined and it is not exporting the functions so I get unresolved externals.

    And this is even stranger. When I load the project up by itself the define works and everything is normal and the second define is greyed out like one would expect. Even stranger yet is that any macros that are after the first #ifdef do not work. No greying out, nothing - almost as if the pre-processor is ignoring them.

    I've tried:
    • Deleting all build files
    • Clean/Rebuild
    • Deleting the ncb
    • Removing the project from the solution and adding it back in
    • Closing and re-starting MSVS
    • Rebooting the PC and restarting MSVS

    I was changing dependencies prior to this happening so I'm wondering if MSVS messed up the solution file (perhaps duplicate entries or something) that is hosing the pre-processor. I've done a search and examined the properties for all projects in the sln for MYDLL_EXPORTS to see if one of them #undef it or did something odd. Nothing. It is not being used anywhere else except in the file it is supposed to be in. Looks like I'm going to be doing a diff on solution files....yay me.

    What is it with the pre-processor ills for me this week?
    Last edited by VirtualAce; 01-12-2010 at 01:14 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    What the IDE greys out and what not is not a good indicator for what's actually happening. The IDE's code help system uses a different parser (or actually, I think two or three different parsers) from the compiler.

    Preprocessor directives in headers are especially tricky, because the defines may differ depending on the include path. For example, if another of your projects uses this DLL header, then the IDE might pick up the path from that project, which doesn't define the symbol.

    That much said, if I understand your post correctly, you actually get problems when doing the real compilation. No idea why that might be happening.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Nor do I. I just force-synced the sln from Perforce and it works - but it did not fix the grey out problem. However it is now exporting the functions so everything works. Very strange error. Definitely one of those 'just move on' and forget it happened types.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  4. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM