Starting from 1.8, one can use TORCH_VERSION_{MAJOR,MINOR,PATCH} for this.
#include <torch/torch.h>
#include <iostream>

int main() {
  std::cout << "PyTorch version: "
    << TORCH_VERSION_MAJOR << "."
    << TORCH_VERSION_MINOR << "."
    << TORCH_VERSION_PATCH << std::endl;
}

Prints:
PyTorch version: 1.8.0