开发者

Wrapping #include directives with other preprocessor commands

I'm working on a project that uses lots of external libraries. Now, I like my projects to compile with the highest warning level available, which on MSVC is /W4. I'm also using -Wall on GCC for this project, which needs to be cross-platform.

With /W4, I'm hitting a problem: I'm often including the headers of the other libraries, and those headers were not written to conform to /W4 (that is, they throw lots of warnings). There's nothing I can do about them so I'd rather not see them. They just end up as noise.

Currently I'm doing this to temporarily lower the warning level:

#if defined(_MSC_VER)
#  开发者_C百科 pragma warning( push, 3 )
#endif

#include <SomeExternalHeader.h>

#if defined(_MSC_VER)
#   pragma warning( pop )
#endif

Including these six lines for every (different) group of external headers I want to include across the various files is getting tiresome. Can I somehow #define a macro that I can then use for these external headers? I'd like something like the following:

INCLUDE_EXTERNAL_HEADER(<SomeExternalHeader.h>)

This would then wrap the #include with the required lines. Is this possible? My gut instinct tells me that wouldn't work because it would require running the preprocessor twice.

What would be the best way to deal with this problem?


I'm not aware of a single line solution like the one you want but MSVC does have a __pragma keyword that you can use inside a preprocessor macro.

So you can do something like this:

#if defined(_MSC_VER)
# define SET_LOWER_WARNING_LEVEL       __pragma(warning(push, 3))
# define RESET_DEFAULT_WARNING_LEVEL   __pragma(warning(pop))
#else 
# define SET_LOWER_WARNING_LEVEL       //gcc directive 
# define RESET_DEFAULT_WARNING_LEVEL   //gcc directive
#endif

SET_LOWER_WARNING_LEVEL
#include "HeaderGeneratingWarnings.h"
RESET_DEFAULT_WARNING_LEVEL

P.S.: I've never used the __pragma form so I don't know for sure whether the pragma directive syntax above is correct.


Wrap ExternalHeader.h in MyExternalHeader.h?


Unfortunately, I don't think MSVC provides for a nice way to deal with this problem. I think you'll either need to continue explicitly turning warnings off/on when these headers are included (maybe using pragma macros to make it a bit easier as Praetorian suggested) or wrap the headers in your own headers that manage the warning level. The second option has the advantage of only needing to be done once. You can do this in a way that doesn't require you to modify each file that includes the original headers.

Say that HeaderGeneratingWarnings.h is in the directory ExtLibDir which your project/makefile is configured to add to the compiler's include search path. One thing you might consider doing is add a 'parallel' directory, ExtLibDirWrapper and use that in the project/makefile configuration. Have it contain a set of headers that look something like:

  • ExtLibDir\HeaderGeneratingWarnings.h:

    #ifndef HEADERGENERATINGWARNINGS_WRAPPER
    #define HEADERGENERATINGWARNINGS_WRAPPER
    
    #if defined(_MSC_VER)
    #   pragma warning( push, 3 )
    #endif
    
    #include "../ExtLibDir/SomeExternalHeader.h"
    
    #if defined(_MSC_VER)
    #   pragma warning( pop )
    #endif
    
    #endif /* HEADERGENERATINGWARNINGS_WRAPPER */
    

An initial set of pain, but a pretty mechanical process - a script could probably be quickly knocked out to take care of the whole thing. Hopefully it would take care of the problem from that point onward.

As an aside, GCC has a nice way to deal with this problem:

  • http://gcc.gnu.org/onlinedocs/cpp/System-Headers.html

The header files declaring interfaces to the operating system and runtime libraries often cannot be written in strictly conforming C. Therefore, GCC gives code found in system headers special treatment. All warnings, other than those generated by `#warning' (see Diagnostics), are suppressed while GCC is processing a system header. Macros defined in a system header are immune to a few warnings wherever they are expanded. This immunity is granted on an ad-hoc basis, when we find that a warning generates lots of false positives because of code in macros defined in system headers.

Normally, only the headers found in specific directories are considered system headers. These directories are determined when GCC is compiled. There are, however, two ways to make normal headers into system headers.

The -isystem command line option adds its argument to the list of directories to search for headers, just like -I. Any headers found in that directory will be considered system headers.

All directories named by -isystem are searched after all directories named by -I, no matter what their order was on the command line. If the same directory is named by both -I and -isystem, the -I option is ignored. GCC provides an informative message when this occurs if -v is used.

There is also a directive, #pragma GCC system_header, which tells GCC to consider the rest of the current include file a system header, no matter where it was found. Code that comes before the #pragma in the file will not be affected. #pragma GCC system_header has no effect in the primary source file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜