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:
/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();
/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(
/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(
| ^~~~~~~~~~~
/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 throwing these errors?
CodePudding user response:
After many tries I have managed to bind the four libraries together and make them work. There were many issues that had to be solved even after solving the error mentioned in the original question. I will describe in short what I did such that if anyone will ever face this issue to know how to solve it. There are many conflicts between qt pcl and libtorch due to methods or structures with the same names.
First I removed all
using namespace someLibrary;
from the code and used the scope resolution operator in front of every function or structure called from a certain library(i.e.someLibrary::some_function()
).I added all the contents related to libtorch in a shared library. Here one should note that all the libtorch files and directories that were mentioned in the CMakeLists.txt file of the library have to be present in the CMakeLists.txt of the main project file.
The guards mentioned bellow have to be added in the files where libtorch is used. These will remove the errors like the ones from the original question. The errors are caused by qt slots conflicting with libtorch structures with the same name.
#undef slots #include <torch/torch.h> #include <torch/script.h> #define slots Q_SLOTS
All the deep learning operations that were related to libtorch were added in a class and each of them had the static keyword in front (so I made them static functions). The operations were then called like any other static method from a class : "className::libtorchOperation()".
I had to add the
${TORCH_INCLUDE_DIRS}
to the include directories of the CMakeLists files and also had to add the following line in the main CMakeLists.txtset(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -luuid")
. One link that helped is Here
And that was about it...It fixed the errors from the original question and made the four frameworks function together.