Pytorch C++ Frontend Example

Hello I am beginner in pytorch c++, in here I find very simple tutorial code. However right now I am using Anaconda to install pytorch and I need to activate my conda environment when I am using it. I install pytorch 1.0 and also cmake in my environment. Then I am trying to compile the source using cmake but I got this error.


-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
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.


-- Configuring incomplete, errors occurred!
See also "/media/mspl/17C9559C215E2AF1/Pytorch_CPP/examples-cpp/cpp/mnist/build/CMakeFiles/CMakeOutput.log".

I have tried to find the solution but most of them tell me to activate the environment, and basically I have done it before, but it still produce the same error.

1 Like

Hi, i think you need download libtorch below compiling:

cmake -DCMAKE_PREFIX_PATH=absolute_path_of_libtorch ..

@tomguluson92 How to know our absolute libtorch in anaconda environment?

libtorch is independent from anaconda environment, no need to care about this.

May be u need learn a little about how to write a CMakeLists.txt for your own mission. I am not familiar with that, sorry.

Sorry, I just forget to correct the my CmakeList. Thank you!

  1. download from here,
  2. unzip it and place in your project folder
  3. run this
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH={project folder}/libtorch ..
1 Like