The proper approach is to get the library fixed to not define a macro that is likely to clash with many other libraries (or don't use a macro at all, come into the 21st century and use a constant, which if properly namespaced can even still be called OK
).
However if that isn't possible you can at least limit the damage by using #undef
:
#include <iostream>
#include "base.h"
#ifdef OK
#if OK != 0
#error "unexpected value of OK"
#endif
#undef OK
namespace mylib
{
const int OK = 0;
}
#endif
#include "grpc.h"
int main() {
std::cout << "hello world\n";
std::cout << grpc::StatusCode::OK << "\n";
std::cout << mylib::OK << "\n";
return 0;
}
Note that if you include some other header that needs OK
after you've done the #undef
it'll obviously not work so you need to be careful of your #include
ordering if that is the case.
Declaring the OK
constant in the global namespace may even allow other code which depends on the OK
macro to compile correctly but is likely (though less likely than a macro) to cause clashes with other libraries.