Ran into a problem in MSVC 2008 today that I cannot explain.
Take the following sample code:
MYDLL_EXPORTS is defined in the project pre-processor settings.
#define MYDLL_DECL __declspec(dllexport)
#define MYDLL_DECL __declspec(dllimport)
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.
- Deleting all build files
- 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?
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.
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.