Libtorch throws some errors when using toghether with qt, pcl and opencv

I am trying to use libtorch, qt widgets, point cloud library(pcl) and opencv in a project. For this project I am using cmake lists. The issue is that when I am using all four libraries together errors are thrown by libtorch. If I use libtorch, opencv and qt everything works fine, also if I use pcl qt and opencv everything also works well. The errors that I get are listed bellow:

  1. /libtorch/include/torch/csrc/jit/api/object.h: In member function ‘size_t torch::jit::Object::num_slots() const’:
    /libtorch/include/torch/csrc/jit/api/object.h:173:28: error: expected unqualified-id before ‘(’ token 173 return _ivalue()->slots().size();
  2. /libtorch/include/ATen/core/ivalue_inl.h: In member function ‘c10::intrusive_ptr c10::IValue::toCustomClass() const &’:
    /libtorch/include/ATen/core/ivalue_inl.h:1642:3: error: expected unqualified-id before ‘(’ token
    1642 | TORCH_CHECK(
  3. /libtorch/include/ATen/core/ivalue_inl.h: In member function ‘c10::intrusive_ptr c10::IValue::toCustomClass() &&’:
    /libtorch/include/ATen/core/ivalue_inl.h:1624:3: error: expected unqualified-id before ‘(’ token
    1624 | TORCH_CHECK(
    | ^~~~~~~~~~~
  4. /libtorch/include/ATen/core/ivalue_inl.h:1419:36: error: expected unqualified-id before ‘)’ token
    1419 | const std::vector& slots() const {

Does anyone know why libtorch is trowing these errors?

Hi. I have same issue when use only Qt and Torch in same build. Furthermore compile error appears only when specific Qt classes included in main.cpp. For the instance:

  • compiles successfully
#include <QString>
#include <torch/script.h>
  • does not compiles (…ivalue_inl.h:1419:36: error: expected unqualified-id before ‘)’ token…)
#include <QFileInfo>
#include <torch/script.h>

So, I have tried to include Qt’s classes after Torch and now my project compiles fine

  • compiles successfully
#include <torch/script.h>
#include <QFileInfo>

It seems that the problem is in the order of the inclusitions

I have solved this issue. Bellow you can find the solution that worked for me.