写在前面
自己的测试环境:
Ubuntu20.04
一、问题描述
自己编译调用 Torch
的程序时,遇到如下报错:
Make Error at CMakeLists.txt:15 (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.
二、解决方法
出现上述报错的原因是没有找到已经安装的 Torch
,因此 需要手动指定路径。
我这里已经在 anaconda3
的虚拟环境 pytorch_env
环境中,安装了 pytorch
, 因此我这里在运行cmake ..
之前,首先运行下述指令来指定`pytorch环境:
export Torch_DIR=/home/wong/ProgramFiles/anaconda3/envs/pytorch_env/lib/python3.8/site-packages/torch/share/cmake/Torch
然后再次运行 cmake ..
即可运行成功。
问题:上述Torch_DIR
对应的路径是怎么找到的呢?
回答:笔者是这样寻找的。根据参考链接,首先使用Ubuntu
文件夹中的搜索
功能,搜索torch
,然后在搜索结果中寻找 torch
文件夹在包含site-packages/
路径的结果,然后进入并寻找是否满足类似于这样格式的路径/site-packages/torch/share/cmake/Torch
,找到类似于这样的路径,即为自己要找的Torch_DIR
路径,然后协商全局路径,并执行上述指令,即可解决问题。
参考链接
[1] Stack Overflow用户. fbtorch: cmake找不到torch目录 [EB/OL]. https://cloud.tencent.com/developer/ask/sof/104640978, 2019-01-28/2024-07-08.