0

I am migrating a windows driver project from VS 2005 to VS 2012. Many macro redefinition warnings are generated on VS 2012 like -

....

1>C:\WINDDK\7600.16385.1\inc\api\sal.h(707): warning C4005: '__format_string' : 
                                                                macro redefinition
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\sal.h(2860) : 
                                       see previous definition of '__format_string'

.....

It was compiling fine with sal.h shipped with VS 2005 because it doesn't have the macro __format_string and others. However, the sal.h shipped with VS 2012 has these macros. Thus having conflicts between the driver's sal.h and the standard sal.h with VS 2012.

#define __format_string                            // With DDK
#define __format_string    _Printf_format_string_  // On VS 2012

I cannot ignore the standard headers because they are used in the build process.

....
1> Note: including file:  C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\string.h
1> Note: including file:  C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\crtdefs.h
....

There is no #if directive around these macros in sal.h so that I can #undef it in VS 2012. Is there any work around for this issue ?

Thanks.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • Do you need to use both the VS' macros _and_ your own macros in the same project, or do you just want to get rid of the warnings? – SingerOfTheFall Sep 20 '12 at 16:22
  • In neither case, they are my own macros. Both are coming from standard header `sal.h` but from different include paths( WinDDK, VS 2012 Standard Include Path ) . And yes, I want to get rid of warnings not by saying to ignore this warning. But with a more formal way, if exists. – Mahesh Sep 20 '12 at 16:25
  • 1
    The more formal way to get rid of the warnings is to add a #undef before each macro definition. But that will end up producing its own warnings if the macro being undef'd hasn't been defined. It's perfectly legal to do that, but compiler writers apparently figure that you're not smart enough to have meant to do it. Unfortunately, doing that means editing system headers, which some people frown on. – Pete Becker Sep 20 '12 at 16:30
  • @PeteBecker I don't like modifying standard headers and neither of my team members :( – Mahesh Sep 20 '12 at 16:38
  • Update the DDK version so it is compatible with VS2012: http://msdn.microsoft.com/en-US/windows/hardware/hh852362 – Hans Passant Sep 20 '12 at 16:57
  • @HansPassant The problem with the latest DDK version, if I amn't wrong, is that it doesn't support to build drivers for `xp` from IDE. Hence I was resorting to the old DDK version. Am I wrong ? – Mahesh Sep 20 '12 at 17:00
  • 1
    VS2012 doesn't support XP right now. – Hans Passant Sep 20 '12 at 17:03
  • @HansPassant Thanks for the confirmation :) – Mahesh Sep 20 '12 at 17:04

2 Answers2

2

Well if I understood what you want correctly, all you need to do is add

#ifdef __format_string 
#undef __format_string
#endif

before the redefinitions.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • 1
    Correct. But there are at least 50 macro redefinition warnings coming from standard headers. And this makes me to modify standard headers. I was looking if there is any include path tweaking to get rid of these warnings. Modifying standard headers is the last resort, which I don't like anyway :) – Mahesh Sep 20 '12 at 16:37
  • @Mahesh, you could add the `#undef`s for all the macros into one special header, and `#include` it right after you `#include` the standard header. At least you will have to only write one line each time. – SingerOfTheFall Sep 20 '12 at 16:45
  • Nice idea. Linked to the second part of this [Paranoia](https://stackoverflow.com/questions/14363929/vs2012-c-warning-c4005-useheader-macro-redefinition/41101907#41101907). :) – Laurie Stearn Feb 05 '18 at 11:40
2

You shouldn't include the VS standard headers in driver code, they aren't for kernel usage. Use WDK headers only.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85