Help building cpp code with opencv

I run into the following error when I try to compile a modified version of this project:

I’m using the unzipped libtorch from Pytorch’s website. And here’s my CMakeLists.txt

make_minimum_required(VERSION 3.0 FATAL_ERROR)

find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)

add_executable(cnn-inference ../../utils/ ../../utils/

target_link_libraries(cnn-inference "${TORCH_LIBRARIES}")
target_link_libraries(cnn-inference "${OpenCV_LIBS}")

message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

set_property(TARGET cnn-inference PROPERTY CXX_STANDARD 11)
#set_property(TARGET cnn-inference PROPERTY OUTPUT_NAME predict)


Could someone help me with these linkage errors? Do I need to compile libtorch from source?

Thanks! :slight_smile:

This can be due to ABI-incompatibility issue. You might want to try compiling libtorch from source, and make sure you are using the same compiler to compile both opencv and libtorch.

We now have new ABI binaries for libtorch. They can be found on, or at It should fix the problem.

See my response here:

I used a pre-compiled OpenCV version. This is the only solution that worked for me.