Libtorch CMakeLists.txt Building Error

Whenever I try to build my CMakeLists.txt file, it says that "CMake Error at CMakeLists.txt:4 (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."

I do not know how to fix this. It would be really helpful if someone could help me.

1 Like

I would recommend to follow this tutorial, which gives you a quick introduction to using libtorch.
As the tutorial and the error message explain, you should use:

root@fa350df05ecf:/home# mkdir build
root@fa350df05ecf:/home# cd build
root@fa350df05ecf:/home/build# cmake -DCMAKE_PREFIX_PATH=/path/to/libtorch ..

to build the application.

2 Likes

Basically I am using CLion and I want to run the program from there. When I use the run feature, it says that the TorchConfig.cmake file is missing

I don’t know, if CLion can build cmake applications, as I’ve never used it.
If not, you would have to build it using the posted instruction.

I get the same error as OP exactly when running cmake from the command line, as copied and pasted from the posted tutorial (Installing C++ Distributions of PyTorch).

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.

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.

  • Where is FindTorch.cmake located?

  • What is CMAKE_MODULE_PATH? An environment variable? A CMAKE variable we can set like set(CMAKE_MODULE_PATH "some/path/")

  • What is the installation “prefix” (in this specific case, but also generally), and is CMAKE_PREFIX_PATH a variable we set in CMakeLists.txt, or is it an environment variable?

I was getting the same error, despite following official the tutorial for installing libtorch. I was able to resolve it by adding the following line to my CMakeLists.txt:

set(Torch_DIR path/to/libtorch/source/libtorch-win-shared-with-deps-2.0.1+cu118/libtorch/share/cmake/Torch)

This is where the file TorchConfig.cmake is located if you have downloaded libtorch as instructed by the tutorial.

1 Like