Torch complitation by catkin

Hi try to use a pytorch model in a c++ file.

I compile my package with catkin_make. Then I got the error:

CMake Error at data_extraction/CMakeLists.txt:27 (find_package):
  By not providing "FindTorch.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Torch", but
  CMake did not find one.

  Could not find a package configuration file provided by "Torch" with any of
  the following names:

    TorchConfig.cmake
    torch-config.cmake

  Add the installation prefix of "Torch" to CMAKE_PREFIX_PATH or set
  "Torch_DIR" to a directory containing one of the above files.  If "Torch"
  provides a separate development package or SDK, be sure it has been
  installed.


-- Configuring incomplete, errors occurred!
See also "/home/wai043/catkin_ws/build/CMakeFiles/CMakeOutput.log".
See also "/home/wai043/catkin_ws/build/CMakeFiles/CMakeError.log".
Makefile:1748: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1
Invoking "make cmake_check_build_system" failed

I also tried these two commands:

export Torch_DIR=/home/user/.local/lib/python3.6/site-packages/torch/share/cmake/Torch/
export CMAKE_PREFIX_PATH=/home/user/.local/lib/python3.6/site-packages/torch/share/cmake/Torch/

but still get the same error.

Does some knows how to solve this?

Best
Gab

In the meanwhile find_package(Torch REQUIRED) was sucessful. I did this:

wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip
unzip libtorch-shared-with-deps-latest.zip

But know I got the following error, if I want to import the torch:

fatal error: torch/script.h: No such file or directory
 #include <torch/script.h> // One-stop header.
          ^~~~~~~~~~~~~~~~
compilation terminated.

Also I did this guide https://pytorch.org/cppdocs/installing.html, which worked. But I would like to compile a larger project with catkin_make.

Has anyone experience with that?

If I add torch libaries: target_link_libraries(pointcloud "${catkin_LIBRARIES}" "${TORCH_LIBRARIES}") then I get the following error:

catkin_make
Base path: /home/wai043/catkin_ws
Source space: /home/wai043/catkin_ws/src
Build space: /home/wai043/catkin_ws/build
Devel space: /home/wai043/catkin_ws/devel
Install space: /home/wai043/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/wai043/catkin_ws/build"
####
####
#### Running command: "make -j12 -l12" in "/home/wai043/catkin_ws/build"
####
[  7%] Built target laserscan_to_pointcloud
[ 14%] Built target matching
[ 21%] Built target pointcloud_to_laserscan
Scanning dependencies of target pointcloud
[ 25%] Linking CXX executable /home/wai043/catkin_ws/devel/lib/extraction/pos_imu
[ 35%] Built target write
[ 42%] Built target laserscan_to_pointcloud_node
[ 57%] Built target read
[ 64%] Built target pointcloud_to_laserscan_node
[ 67%] Building CXX object data_extraction/CMakeFiles/pointcloud.dir/src/pointcloud.cpp.o
CMakeFiles/pos_imu.dir/src/pos_imu.cpp.o: In function `main':
pos_imu.cpp:(.text+0xfb): undefined reference to `ros::init(int&, char**, std::string const&, unsigned int)'
pos_imu.cpp:(.text+0x18d): undefined reference to `ros::NodeHandle::NodeHandle(std::string const&, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&)'
CMakeFiles/pos_imu.dir/src/imu_extraction/imu.cpp.o: In function `ros::SubscriptionCallbackHelperT<boost::shared_ptr<sensor_msgs::Imu_<std::allocator<void> > const> const&, void>::deserialize(ros::SubscriptionCallbackHelperDeserializeParams const&)':
imu.cpp:(.text._ZN3ros27SubscriptionCallbackHelperTIRKN5boost10shared_ptrIKN11sensor_msgs4Imu_ISaIvEEEEEvE11deserializeERKNS_43SubscriptionCallbackHelperDeserializeParamsE[_ZN3ros27SubscriptionCallbackHelperTIRKN5boost10shared_ptrIKN11sensor_msgs4Imu_ISaIvEEEEEvE11deserializeERKNS_43SubscriptionCallbackHelperDeserializeParamsE]+0xaf): undefined reference to `ros::console::initializeLogLocation(ros::console::LogLocation*, std::string const&, ros::console::levels::Level)'
CMakeFiles/pos_imu.dir/src/pos_extraction/position.cpp.o: In function `ros::SubscriptionCallbackHelperT<boost::shared_ptr<geometry_msgs::PoseStamped_<std::allocator<void> > const> const&, void>::deserialize(ros::SubscriptionCallbackHelperDeserializeParams const&)':
position.cpp:(.text._ZN3ros27SubscriptionCallbackHelperTIRKN5boost10shared_ptrIKN13geometry_msgs12PoseStamped_ISaIvEEEEEvE11deserializeERKNS_43SubscriptionCallbackHelperDeserializeParamsE[_ZN3ros27SubscriptionCallbackHelperTIRKN5boost10shared_ptrIKN13geometry_msgs12PoseStamped_ISaIvEEEEEvE11deserializeERKNS_43SubscriptionCallbackHelperDeserializeParamsE]+0xaf): undefined reference to `ros::console::initializeLogLocation(ros::console::LogLocation*, std::string const&, ros::console::levels::Level)'
collect2: error: ld returned 1 exit status
data_extraction/CMakeFiles/pos_imu.dir/build.make:366: recipe for target '/home/wai043/catkin_ws/devel/lib/extraction/pos_imu' failed
make[2]: *** [/home/wai043/catkin_ws/devel/lib/extraction/pos_imu] Error 1
CMakeFiles/Makefile2:1221: recipe for target 'data_extraction/CMakeFiles/pos_imu.dir/all' failed
make[1]: *** [data_extraction/CMakeFiles/pos_imu.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
/home/wai043/catkin_ws/src/data_extraction/src/pointcloud.cpp: In function ‘int main(int, char**)’:
/home/wai043/catkin_ws/src/data_extraction/src/pointcloud.cpp:30:5: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
     while (std::getline(filenumberoflines, line))
     ^~~~~
/home/wai043/catkin_ws/src/data_extraction/src/pointcloud.cpp:33:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘while’
  if (parameter_line_configuration > number_of_lines-1){
  ^~
[ 71%] Linking CXX executable /home/wai043/catkin_ws/devel/lib/extraction/pointcloud
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
/usr/bin/ld: cannot find -loptimized
/usr/bin/ld: cannot find -ldebug
collect2: error: ld returned 1 exit status
data_extraction/CMakeFiles/pointcloud.dir/build.make:678: recipe for target '/home/wai043/catkin_ws/devel/lib/extraction/pointcloud' failed
make[2]: *** [/home/wai043/catkin_ws/devel/lib/extraction/pointcloud] Error 1
CMakeFiles/Makefile2:3215: recipe for target 'data_extraction/CMakeFiles/pointcloud.dir/all' failed
make[1]: *** [data_extraction/CMakeFiles/pointcloud.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j12 -l12" failed

This topic has been 3 years from now but I still want to point out my solution for this case.

We can append another address location in CMAKE_PREFIX_PATH for libtorch like the image below
Screenshot 2024-03-10 154827