LibTorch

Download PyTorch on this site link . You can choose between CUDA (Compute Unified Device Architecture) and CPU compute platform.

  • CUDA runs on the GPU — ideal for large models and faster training.
  • CPU runs on the processor — suitable for small models and systems without a GPU.
PyTorch download

Best practice: It's a good idea to copy the files to C:/ or another local drive for easier access and compatibility. Then rename the folder to a human-readable name, e.g., libtorch.

In Properties, under C/C++ → General → Additional Include Directories, enter the following paths:

C:\libtorch\include
C:\libtorch\include\torch\csrc\api\include
LibTorch include paths

In C/C++ → Language → Conformance Mode, set the value to No (/permissive). This disables strict standard conformance and helps avoid issues with ambiguous namespaces in some libraries.

In C/C++ → Language → C++ Language Standard, choose C++17 or a newer version. This is required for modern libraries like LibTorch to compile correctly.

LibTorch language settings

In Linker → General → Additional Library Directories, enter the following path:

C:\libtorch\lib
LibTorch libraries

In Linker → Input → Additional Dependencies, add the following libs from the path C:\\libtorch\\lib :

torch.lib
torch_cpu.lib
c10.lib
LibTorch libraries

In Build Events → Pre-Build Event → Command Line, add the following command:

xcopy /Y /C /I /Q "C:\libtorch\lib\*.dll" "$(OutDir)"
Pre-Build command

OR: You can copy them manually :D

If torch::cuda::is_available() returns false in Visual Studio even though CUDA Toolkit and GPU are properly installed, you must force the CUDA section to be linked into the final binary.

/INCLUDE:"?warp_size@cuda@at@@YAHXZ"

Add the following flag in your project properties under Linker → Command Line → Additional Options. This ensures the linker keeps the CUDA initialization code so LibTorch correctly detects your GPU.

  • Without this flag, Visual Studio often discards unused CUDA symbols during linking.
  • As a result, the CUDA part of torch_cuda.lib never gets linked into your executable.
  • torch::cuda::is_available() then always returns false, even if your GPU works fine.

Better solution: If you use CMake, prefer the official approach with find_package(Torch REQUIRED). It automatically links the necessary libraries and avoids the manual /INCLUDE parameter.