Download PyTorch on this site link . You can choose between CUDA (Compute Unified Device Architecture) and CPU compute platform.
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.
C:/
In Properties, under C/C++ → General → Additional Include Directories, enter the following paths:
C:\libtorch\include C:\libtorch\include\torch\csrc\api\include
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.
In Linker → General → Additional Library Directories, enter the following path:
C:\libtorch\lib
In Linker → Input → Additional Dependencies, add the following libs from the path C:\\libtorch\\lib :
torch.lib torch_cpu.lib c10.lib
In Build Events → Pre-Build Event → Command Line, add the following command:
xcopy /Y /C /I /Q "C:\libtorch\lib\*.dll" "$(OutDir)"
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.
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.