LibTorch & Abseil LOG() conflict

Hello. I am using libtorch in a large C++ project which heavily uses Abseil. It looks like simply including <torch/torch.h> introduces these macros (LOG(), VLOG(), CHECK(), etc.) into my project, and if they already existed from absl, overwrites them.

Given that libtorch is meant to be used as a library, can you please not pollute the global namespace with these macros?

As far as I understand, there are a few workarounds:

  1. Refactor my existing project to use ABSL_LOG, ABSL_VLOG, etc… to avoid the conflict
  2. #undef the libtorch macros
  3. Diligently always include absl headers after torch headers

Please see the below repository which shows a minimal reproducer and describes the issue in a bit more detail.

After further consideration, #3 is not feasible. In a large project, always perfectly getting the include order right is very difficult to maintain, especially when the issue does not result in a compile time error but instead a slight difference in logging.

#2 is not bad, I could use a “compat” header to help with this issue:

#include <absl/log/absl_log.h>
#include <torch/torch.h>
#ifdef LOG
#  undef LOG
#endif
#define LOG(severity) ABSL_LOG(severity)

#ifdef VLOG
#  undef VLOG
#endif
#define VLOG(level) ABSL_VLOG(level)

I’d need to be sure to only ever include this header and never directly include <torch/torch.h>. There are other macros which slip through in this case, like CHECK, DCHECK, and who knows what else.

In the case of the compat header as well as refactoring my codebase to use ABSL_ versions, there is still the problem that torch headers are leaking macros into the global namespace.