Torch complitation by catkin

gab1: <p>Hi try to use a pytorch model in a c++ file.</p>
<p>I compile my package with catkin_make. Then I got the error:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>I also tried these two commands:</p>
<pre><code class="lang-auto">export Torch_DIR=/home/user/.local/lib/python3.6/site-packages/torch/share/cmake/Torch/
</code></pre>
<pre><code class="lang-auto">export CMAKE_PREFIX_PATH=/home/user/.local/lib/python3.6/site-packages/torch/share/cmake/Torch/
</code></pre>
<p>but still get the same error.</p>
<p>Does some knows how to solve this?</p>
<p>Best<br>
Gab</p>

gab1: <p>In the meanwhile <code>find_package(Torch REQUIRED)</code> was sucessful. I did this:</p>
<pre><code class="lang-auto">wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip
unzip libtorch-shared-with-deps-latest.zip
</code></pre>
<p>But know I got the following error, if I want to import the torch:</p>
<pre><code class="lang-auto">fatal error: torch/script.h: No such file or directory
 #include &lt;torch/script.h&gt; // One-stop header.
          ^~~~~~~~~~~~~~~~
compilation terminated.
</code></pre>
<p>Also I did this guide <a href="https://pytorch.org/cppdocs/installing.html" rel="nofollow noopener">https://pytorch.org/cppdocs/installing.html</a>, which worked. But I would like to compile a larger project with catkin_make.</p>
<p>Has anyone experience with that?</p>

gab1: <p>If I add torch libaries: <code>target_link_libraries(pointcloud "${catkin_LIBRARIES}" "${TORCH_LIBRARIES}")</code> then I get the following error:</p>
<pre><code class="lang-auto">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&amp;, char**, std::string const&amp;, unsigned int)'
pos_imu.cpp:(.text+0x18d): undefined reference to `ros::NodeHandle::NodeHandle(std::string const&amp;, std::map&lt;std::string, std::string, std::less&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, std::string&gt; &gt; &gt; const&amp;)'
CMakeFiles/pos_imu.dir/src/imu_extraction/imu.cpp.o: In function `ros::SubscriptionCallbackHelperT&lt;boost::shared_ptr&lt;sensor_msgs::Imu_&lt;std::allocator&lt;void&gt; &gt; const&gt; const&amp;, void&gt;::deserialize(ros::SubscriptionCallbackHelperDeserializeParams const&amp;)':
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&amp;, ros::console::levels::Level)'
CMakeFiles/pos_imu.dir/src/pos_extraction/position.cpp.o: In function `ros::SubscriptionCallbackHelperT&lt;boost::shared_ptr&lt;geometry_msgs::PoseStamped_&lt;std::allocator&lt;void&gt; &gt; const&gt; const&amp;, void&gt;::deserialize(ros::SubscriptionCallbackHelperDeserializeParams const&amp;)':
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&amp;, 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 &gt; 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

</code></pre>

D_t_Le: <p>This topic has been 3 years from now but I still want to point out my solution for this case.</p>
<p>We can append another address location in CMAKE_PREFIX_PATH for libtorch like the image below<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/4/9/496e576fc70cbc1a727d64140951b61f738c608b.png" alt="Screenshot 2024-03-10 154827" data-base62-sha1="atBgbmQ2uNq76SfUoVlBsXwR7Np" width="476" height="145"></p>