1

I'm working on an Objective-C wrapper around a C++ library, whose source I neither control nor modify. The headers I import trigger various compiler warnings, so I've started to do the following:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#pragma clang diagnostic ignored "-Wstrict-prototypes"
#import "dll.hpp"
#pragma clang diagnostic pop

This works great, but to keep it DRY, I'd like to define a macro for each of these imports with the ignored warning pragmas built in (there are only a few headers like this).

This way, in each file I could just make a call like this at the top and I'd have the ignored warnings all in one spot for each header.

ImportDllHpp()

It's not so easy, however, calling #import from within a #define. I've gotten pretty close, using this answer to get the pragmas working. But is there a function like _Pragma for #import or another way to achieve that?

This is what I have so far:

#define _stringify(a) #a

#define ImportRarHpp() \
_Pragma( _stringify( clang diagnostic push ) ) \
_Pragma( _stringify( clang diagnostic ignored "-Wreserved-id-macro" ) ) \
_Pragma( _stringify( clang diagnostic ignored "-Wstrict-prototypes" ) ) \
#import "dll.hpp"
_Pragma( _stringify( clang diagnostic pop ) )
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
Dov
  • 15,530
  • 13
  • 76
  • 177

1 Answers1

2

I would be very surprised if you could do this.

I would suggest the simplest solution is for you to simply define your own file named something like my-dll.hpp which consists of

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#pragma clang diagnostic ignored "-Wstrict-prototypes"
#import "dll.hpp"
#pragma clang diagnostic pop

That way anyone who wants to import dll.hpp will instead just #import "my-dll.hpp" and the appropriate warnings will be suppressed.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • I was thinking of `#include "my-dll.hpp"` instead (I'm not up on C++17 yet), but same concept. Move the `#pragma`s and `#import` to its own file, then include/import that flle where needed – Remy Lebeau Oct 03 '17 at 21:45