How to link LibTorch with a Gradle-based C++ project?

Hello, I am trying to link LibTorch to a C++ codebase that uses Gradle as its build system. Unfortunately, it appears that the vast majority of documentation covers adding LibTorch to a CMake project, not Gradle, and I am running into issues linking the library.

My issue primarily stems from including the headers (even when using the pre-built variant). From the repository we have:

It seems that there are two include directories for LibTorch, one of which is inside the other one. This poses issues for me when I try to provide the headers.srcDir because it appears to only accept one option for a folder path where it can look for the headers.

If I simply copy the headers over to /usr/local/include and provide that path to Gradle, then I am informed it cannot find the inner one. I tried several roundabout ways of doing this, but any time I run into issues where it either (a) cannot find the inner folder, (b) cannot find the outer folder, or (c) states that there are duplicate declarations for overlapping files.

Hence, I am wondering if anyone has an example of integrating it with a Gradle C++ project (or another build tool), possible workarounds, or any suggestions on how to deal with the two nested include folders. Thank you!

I managed to get progress on this front by leveraging CppSourceSet, which is part of the ‘cpp’ plugin for Gradle. This allows for elegantly expressing multiple header file import locations.

Hence, we can simply do:

headers.srcDirs = ["/usr/local/include/torch/csrc/api/include/", "/usr/local/include/torch/"]

This manages to resolve the headers issue.

I cannot confirm if it completely works because I am since running into a C++11 ABI issue, but once I resolve that (which I believe is caused by another problem) I will look back and confirm that it is solved.

Can confirm that this works.