Errors while using Libtorch + OpenCV + QT Creator
Asked Answered
A

2

7

I have the following configuration in the .pro file

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
CONFIG += thread

SOURCES += main.cpp

INCLUDEPATH += /usr/local/include/opencv4
LIBS += -L/usr/local/lib/
LIBS += -lopencv_core
LIBS += -lopencv_highgui
LIBS += -lopencv_imgproc
LIBS += -lopencv_videoio

QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0

INCLUDEPATH += /path/to/libtorch/include
INCLUDEPATH += /path/to/libtorch/include/torch/csrc/api/include
LIBS += -L/path/to/libtorch/lib
LIBS += -ltorch -lc10

OpenCV works absolutely fine without "QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0". With this, however, I get this following errors:

enter image description here

OpenCV works fine with "QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=1" as well. But it throws a different set of errors:

enter image description here

Setting "QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0" has been recommended for Libtorch in most of the forums to avoid the errors above.

What could be a solution or some solutions to work around this? (I am a newbie to both Libtorch and Qt Creator.)

Airmail answered 24/7, 2019 at 13:7 Comment(0)
A
2

Maybe OpenCV and Libtorch were compiled with a different version of GCC (and different values of _GLIBCXX_USE_CXX11_ABI).

Try recompiling them by yourself and see if things change.

Asco answered 29/7, 2019 at 16:56 Comment(0)
E
1

The problem is that you downloaded the wrong ABI version of LibTorch. It looks like that you downloaded the Pre-CXX11 ABI version of LibTorch, while OpenCV is compiled with CXX11 ABI. So if you set _GLIBCXX_USE_CXX11_ABI=0, OpenCV throws errors, and if you set _GLIBCXX_USE_CXX11_ABI=1, LibTorch throws errors.

Download the CXX11 ABI LibTorch from PyTorch official website and you won't need to set the _GLIBCXX_USE_CXX11_ABI flag.

Select CXX11 ABI

About the dual ABI problem of GCC, see https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html

Ecstasy answered 21/4, 2021 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.