Library conflict between libtorch and opencv?

I’m trying to deploy model by C++, I got this error when <torch/script.h> is included in my code:

mat_tensor.cpp:(.text+0x6d): undefined reference to `cv::imread(std::string const&, int)'
mat_tensor.cpp:(.text+0xad): undefined reference to `cv::namedWindow(std::string const&, int)'
mat_tensor.cpp:(.text+0x126): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/mat_tensor.dir/build.make:116: recipe for target 'mat_tensor' failed
make[2]: *** [mat_tensor] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mat_tensor.dir/all' failed
make[1]: *** [CMakeFiles/mat_tensor.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

When I removed torch from my code and CMakeLists.txt, everything words well.

My code:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <torch/script.h>
#include <string>

int main()
{
    std::string p = "/home/dai/scripts/pytorch_chineseocr/pictures/1.png";
    cv::Mat img = cv::imread(p);
    cv::namedWindow("img");
    cv::imshow("img",img);
    cv::waitKey(0);
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(mat_tensor)

SET(OpenCV_DIR /home/dai/library/opencv-2.4.13.6/build)
SET(Torch_DIR /home/dai/library/libtorch_cpu/share/cmake/Torch)
find_package(OpenCV REQUIRED)
find_package(Torch REQUIRED)
add_executable(mat_tensor mat_tensor.cpp)
target_link_libraries(mat_tensor ${OpenCV_LIBRARIES})
target_link_libraries(mat_tensor ${TORCH_LIBRARIES})

My Libtorch version is libtorch1.3+cpu, and OpenCV version is 2.4.13( 2.4.9 doesn’t work either).

This is likely an ABI compatibility issue. Could you try using libtorch binary with new ABI?

Use my docker here:


It resolves the issue. An example is available here:

I met error as following using libtorch binary with new ABI:

In function `c10::tensorTypeIdToBackend(c10::TensorTypeId)':
ocr.cpp:(.text+0x35d): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `c10::backendToDeviceType(c10::Backend)':
ocr.cpp:(.text+0x5c3): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
ocr.cpp:(.text+0x6e8): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `c10::typeMetaToScalarType(caffe2::TypeMeta)':
ocr.cpp:(.text+0xdd5): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `LoadModels(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, torch::jit::script::Module&, torch::jit::script::Module&, torch::jit::script::Module)':
ocr.cpp:(.text+0x135f): undefined reference to `torch::jit::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, c10::optional<c10::Device>, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)'
ocr.cpp:(.text+0x13af): undefined reference to `torch::jit::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, c10::optional<c10::Device>, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)'
ocr.cpp:(.text+0x13fe): undefined reference to `torch::jit::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, c10::optional<c10::Device>, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `c10::Device::validate()':
ocr.cpp:(.text._ZN3c106Device8validateEv[_ZN3c106Device8validateEv]+0x10f): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
ocr.cpp:(.text._ZN3c106Device8validateEv[_ZN3c106Device8validateEv]+0x256): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `c10::operator<<(std::ostream&, c10::Layout)':
ocr.cpp:(.text._ZN3c10lsERSoNS_6LayoutE[_ZN3c10lsERSoNS_6LayoutE]+0x155): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `c10::intrusive_ptr_target::~intrusive_ptr_target()':
ocr.cpp:(.text._ZN3c1020intrusive_ptr_targetD2Ev[_ZN3c1020intrusive_ptr_targetD5Ev]+0x12b): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
ocr.cpp:(.text._ZN3c1020intrusive_ptr_targetD2Ev[_ZN3c1020intrusive_ptr_targetD5Ev]+0x259): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o:ocr.cpp:(.text._ZN3c1011StorageImplC2EN6caffe28TypeMetaElNS_7DataPtrEPNS_9AllocatorEb[_ZN3c1011StorageImplC5EN6caffe28TypeMetaElNS_7DataPtrEPNS_9AllocatorEb]+0x1bf): more undefined references to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' follow
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `c10::Symbol::dimname(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
ocr.cpp:(.text._ZN3c106Symbol7dimnameERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN3c106Symbol7dimnameERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x3b): undefined reference to `c10::Symbol::fromQualString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `c10::IValue::toTensor() &&':
ocr.cpp:(.text._ZNO3c106IValue8toTensorEv[_ZNO3c106IValue8toTensorEv]+0x11d): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `at::ATenDispatch::getOpTable(char const*) const':
ocr.cpp:(.text._ZNK2at12ATenDispatch10getOpTableEPKc[_ZNK2at12ATenDispatch10getOpTableEPKc]+0x180): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `at::Context::getDeviceFromPtr(void*, c10::DeviceType)':
ocr.cpp:(.text._ZN2at7Context16getDeviceFromPtrEPvN3c1010DeviceTypeE[_ZN2at7Context16getDeviceFromPtrEPvN3c1010DeviceTypeE]+0x113): undefined reference to `c10::DeviceTypeName[abi:cxx11](c10::DeviceType, bool)'
ocr.cpp:(.text._ZN2at7Context16getDeviceFromPtrEPvN3c1010DeviceTypeE[_ZN2at7Context16getDeviceFromPtrEPvN3c1010DeviceTypeE]+0x1ab): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `at::from_blob(void*, c10::ArrayRef<long>, c10::ArrayRef<long>, std::function<void (void*)> const&, c10::TensorOptions const&)':
ocr.cpp:(.text._ZN2at9from_blobEPvN3c108ArrayRefIlEES3_RKSt8functionIFvS0_EERKNS1_13TensorOptionsE[_ZN2at9from_blobEPvN3c108ArrayRefIlEES3_RKSt8functionIFvS0_EERKNS1_13TensorOptionsE]+0x1e5): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `torch::autograd::make_variable(at::Tensor, bool, bool)':
ocr.cpp:(.text._ZN5torch8autograd13make_variableEN2at6TensorEbb[_ZN5torch8autograd13make_variableEN2at6TensorEbb]+0x108): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `c10::intrusive_ptr<c10::intrusive_ptr_target, c10::detail::intrusive_target_default_null_type<c10::intrusive_ptr_target> >::reclaim(c10::intrusive_ptr_target*)':
ocr.cpp:(.text._ZN3c1013intrusive_ptrINS_20intrusive_ptr_targetENS_6detail34intrusive_target_default_null_typeIS1_EEE7reclaimEPS1_[_ZN3c1013intrusive_ptrINS_20intrusive_ptr_targetENS_6detail34intrusive_target_default_null_typeIS1_EEE7reclaimEPS1_]+0x145): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o: In function `c10::KernelFunction const& c10::DispatchTable::lookup_<c10::DispatchTable::lookup(c10::TensorTypeId) const::{lambda()#1}>(c10::DispatchTable::lookup(c10::TensorTypeId) const::{lambda()#1} const&) const':
ocr.cpp:(.text._ZNK3c1013DispatchTable7lookup_IZNKS0_6lookupENS_12TensorTypeIdEEUlvE_EERKNS_14KernelFunctionERKT_[_ZNK3c1013DispatchTable7lookup_IZNKS0_6lookupENS_12TensorTypeIdEEUlvE_EERKNS_14KernelFunctionERKT_]+0x1e2): undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/cpp_deploy.dir/ocr.cpp.o:ocr.cpp:(.text._ZNK3c1013DispatchTable7lookup_IZNKS0_6lookupENS_12TensorTypeIdEEUlvE_EERKNS_14KernelFunctionERKT_[_ZNK3c1013DispatchTable7lookup_IZNKS0_6lookupENS_12TensorTypeIdEEUlvE_EERKNS_14KernelFunctionERKT_]+0x3a4): more undefined references to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' follow
tools/libtools.so: undefined reference to `torch::jit::parseSchemaOrName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'

@dalalaa hmm this is weird, did you download https://download.pytorch.org/libtorch/cu101/libtorch-cxx11-abi-shared-with-deps-1.3.1.zip ?

1 Like

I downloaded it from https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.3.1%2Bcpu.zip
And my environment is :

gcc:7.5
Lubuntu:18.04
Python:3.7.4
PyTorch:1.3.1

CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
add_definitions(-std=c++11)
project(cpp_deploy)

set(OpenCV_DIR /home/dai/lib/opencv-2.4.9/build)
set(Torch_DIR /home/dai/lib/libtorch_cxx11/libtorch/share/cmake/Torch)
find_package(OpenCV REQUIRED)
find_package(Torch REQUIRED)

add_subdirectory(tools)
add_executable(cpp_deploy ocr.cpp)
target_link_libraries(cpp_deploy ${TORCH_LIBRARIES})
target_link_libraries(cpp_deploy ${OpenCV_LIBRARIES})
target_link_libraries(cpp_deploy -Wl,--no-as-needed tools)
target_compile_features(cpp_deploy PRIVATE cxx_range_for)

Did this issue resolve? Even I’m facing the same.

@offset-null1 I fixed it using cxx11 ABI instead of Pre-cxx11 ABI

2 Likes

Bingo !!! Thanks a lot !! This is big unrecognizing bug !! After reinstall libtorch, all dependences resolved.
For CUDA reliase too.
Please USE libtorch-cxx11-abi-shared-with-deps-1.10.0+cu102 NOT libtorch-shared-with-deps-1.10.0+cu102.