Proper way of attach LibTorch built from source in C++ project


So far I’ve been using LibTorch downloaded from (pre-built), however I realized that building PyTorch from source on my machine gives me huge speed increase, so I would like to do that with LibTorch too. I built it using these instructions.

Can anyone explain me how to use built from source LibTorch in C++ project? Its structure slightly differs from this downloaded from official website and I get some compilation errors.

Thank you

You can also use your build directly without going through conda:

mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=<whatever>/pytorch/torch/share/cmake ..

Where the pytorch is the top level folder you have the PyTorch repo checked out to (e.g. the folder produced when you run git clone

The fastest way probably from the point where you are at is to just use the LibTorch from the PyTorch you built. It contains all you need in torch/lib, torch/shared, torch/include from the Python installed directory (/usr/local/lib/python*/dist-packages/torch/* for me, try torch.__file__ if you can’t find it).

Best regards


1 Like