Setting Up Libtorch and OpenCV in Visual C++ Projects

Libtorch Setup for C++ Development

Prerequisites

This guide assumes PyTorch 2.1.2 with CUDA (cu118) is already installed on the system.

Installation

  1. Download the Libtorch debug distribution matching your PyTorch version: libtorch-win-shared-with-deps-debug-2.1.2+cu118.zip
  2. Extract the archive to a preferred location on disk.

Important: The Libtorch version must exactly match the PyTorch version installed in Python. Version mismatches cause cryptic runtime errors.

Visual Studio Configuration

System Path

Add the Libtorch library directory to the system PATH environment variable:

<path_to_libtorch>\lib

Project Include Directories

Navigate to Project Properties → VC++ Directories → Include Directories and add:

<path_to_libtorch>\include
<path_to_libtorch>\include\torch\csrc\api\include

Project Library Directories

Navigate to Project Properties → VC++ Directories → Library Directories and add:

<path_to_libtorch>\lib

Compiler Settings

Under C/C++ → Language → Conformance Mode, set to No.

Under C/C++ → General → SDL Checks, set to No.

Under C/C++ → Language → C++ Language Standard, select ISO C++17 Standard (/std:c++17).

Linker Dependencies

Navigate to Linker → Input → Additional Dependencies and add all .lib files from the Libtorch lib directory. The essential libraries include:

asmjit.lib
c10.lib
c10_cuda.lib
caffe2_nvrtc.lib
clog.lib
cpuinfo.lib
dnnl.lib
fbgemm.lib
fbjni.lib
fmtd.lib
kineto.lib
libprotobufd.lib
libprotobuf-lited.lib
libprotocd.lib
pthreadpool.lib
pytorch_jni.lib
torch.lib
torch_cpu.lib
torch_cuda.lib
XNNPACK.lib

Verification

Compile and run the following test program:

#include <torch/torch.h>
#include <torch/script.h>

int main() {
    torch::jit::script::Module module = torch::jit::load(".\\trained_model.pt");
    return 0;
}

OpenCV Setup for C++ Development

Installation

Download and extract OpenCV. This guide uses version 4.5.4, though the configuration steps apply to other recent versions.

Visual Studio Configuration

System Path

Add the OpenCV binaries to the system PATH:

<path_to_opencv>\build\x64\vc15\bin

Project Include Directories

Navigate to Project Properties → VC++ Directories → Include Directories and add:

<path_to_opencv>\build\include
<path_to_opencv>\build\include\opencv2

Project Library Directories

Navigate to Project Properties → VC++ Directories → Library Directories and add:

<path_to_opencv>\build\x64\vc15\lib

Linker Dependencies

Navigate to Linker → Input → Additional Dependencies and add the appropriate library. For debug builds:

opencv_world454d.lib

For release builds:

opencv_world454.lib

Verification

Compile and run the following test program:

#include <opencv2/opencv.hpp>

int main() {
    cv::Mat image = cv::imread("sample_image.jpg");
    cv::imshow("Display Window", image);
    cv::waitKey(0);
    return 0;
}

The window displays the loaded image and waits for a key press before closing.

Tags: libtorch OpenCV C++ Visual Studio Deep Learning

Posted on Sun, 16 Aug 2026 16:38:25 +0000 by masteroleary