PDA

View Full Version : Qt linking with and OpenCV



Zamaster
31st March 2014, 23:55
I'm attempting to use OpenCV 248 within my Qt Creation application and am hitting a linking snag. In the .pro file I have the following defined:



#--------------- specifying opencv libs -----------------

CV_LIB_PATH = ../../Libs/x64/opencv/build/x64/vc12/staticlib/
CV_VERSION = 248
CV_LIBS += opencv_core \
opencv_features2d \
opencv_highgui \
opencv_imgproc \


#--------------------------------------------------------


CV_DEBUG =
debug {
CV_DEBUG += d
}

cvpaths =
cvlibs =
for(libName, CV_LIBS){
cvpaths += -L$${CV_LIB_PATH}$${libName}$${CV_VERSION}$${CV_DEB UG}
cvlibs += -l$${libName}$${CV_VERSION}$${CV_DEBUG}
}

INCLUDEPATH += ../../Includes \
../../Includes/opencv/includes \
LIBS += $${cvpaths} $${cvlibs}


-and qmake seems to find everything just fine, no errors. Then in an unassuming object file:



#include "autoslicer.h"
#include "opencv2/core/core.hpp"

Autoslicer::Autoslicer()
{
cv::Mat2d a;

}


It finds the include just fine. So I know I've got my path's right.
When I go to build I get a bunch of unresolved's for instantiating that Mat2d. What did I miss? Thanks!

Added after 1 32 minutes:

So I did notice that I had an extra "\" after my last include path line, fair enough. After checking for other inconsistencies, I ran again, and now Qt is hitting me with a linker 1104, "cannot open output file 'opencv_core248d.lib".

This puzzled me as the CV_LIB_PATH variable I created seemed correct. So then I put the static libs in with the .pro and changed CV_LIB_PATH to "". (NOTE: CV_LIB_PATH is my own variable I define in the .pro file) Still Qt cannot locate the file (and I would imagine the others). Is there anything glaringly wrong that I did?

ChrisW67
1st April 2014, 01:24
You are using a relative path to the libraries. If you are also doing a shadow build this will result in the relative path being interpreted with respect to the build directory and not the source directory. This can lead to libraries not being found. Try an absolute path as an experiment.

The message "linker 1104, "cannot open output file 'opencv_core248d.lib' " is puzzling since this file is an input not an output. This would seem to indicate that the generated linker command line is broken in some way. I cannot see an obvious problem in your PRO file extract.

Zamaster
1st April 2014, 03:16
Good call, I tried an absolute path and I'm getting the same error. Just to see, I turned on verbose for qmake and checked the "LIBS += ..." line. Sure enough, the path to the libraries was correct:



LIBS := -LC:/Users/Zamaster/Dropbox/Softwa~1/Mop!/dev/Libs/x64/opencv/build/x64/vc12/staticlib/opencv_core248d -LC:/Users/Zamaster/Dropbox/Softwa~1/Mop!/dev/Libs/x64/opencv/build/x64/vc12/staticlib/opencv_features2d248d -LC:/Users/Zamaster/Dropbox/Softwa~1/Mop!/dev/Libs/x64/opencv/build/x64/vc12/staticlib/opencv_highgui248d -LC:/Users/Zamaster/Dropbox/Softwa~1/Mop!/dev/Libs/x64/opencv/build/x64/vc12/staticlib/opencv_imgproc248d -lopencv_core248d -lopencv_features2d248d -lopencv_highgui248d -lopencv_imgproc248d


and just for full context the current .pro:




QT += core gui opengl

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = RegionsBuilder2
TEMPLATE = app

#--------------- specifying opencv libs -----------------

CV_LIB_PATH = C:/Users/Zamaster/Dropbox/Softwa~1/Mop!/dev/Libs/x64/opencv/build/x64/vc12/staticlib/
CV_VERSION = 248
CV_LIBS += opencv_core \
opencv_features2d \
opencv_highgui \
opencv_imgproc \


#--------------------------------------------------------


CV_DEBUG =
debug {
CV_DEBUG += d
}

cvpaths =
cvlibs =
for(libName, CV_LIBS){
cvpaths += -L$${CV_LIB_PATH}$${libName}$${CV_VERSION}$${CV_DEB UG}
cvlibs += -l$${libName}$${CV_VERSION}$${CV_DEBUG}
}

INCLUDEPATH += ../../Includes \
../../Includes/opencv/includes

LIBS += $${cvpaths} $${cvlibs}

SOURCES += main.cpp\
mainwindow.cpp \
myglwidget.cpp \
glminimap.cpp \
glzmap.cpp \
qwidget_slicesheet.cpp \
gldrawsurface.cpp \
spritemachine.cpp \
primitivemachine.cpp \
slicesheet_preview.cpp \
autoslicer.cpp

HEADERS += mainwindow.h \
myglwidget.h \
glzmap.h \
glminimap.h \
qwidget_slicesheet.h \
gldrawsurface.h \
spritemachine.h \
primitivemachine.h \
slicesheet_preview.h \
autoslicer.h

FORMS += mainwindow.ui \
qwidget_slicesheet.ui

RESOURCES += \
res.qrc \
builtin_shaders.qrc

OTHER_FILES += \
lineart.vert \
lineart.frag \
sprite.vert \
sprite.frag

ChrisW67
1st April 2014, 04:15
My bad. The -L option should specify a directory. If all the library files are in the same directory (I suspect they are) then you should have only one -L and many -l options:

LIBS += -LC:/Users/Zamaster/Dropbox/Softwa~1/Mop!/dev/Libs/x64/opencv/build/x64/vc12/staticlib
LIBS += -lopencv_core248d -lopencv_features2d248d -lopencv_highgui248d -lopencv_imgproc248d
You would need to adjust (simplify) your PRO file accordingly .

The alternative is to specify the full path and name of the libraries without any -L or -l options:


LIBS += C:/Users/Zamaster/Dropbox/Softwa~1/Mop!/dev/Libs/x64/opencv/build/x64/vc12/staticlib/opencv_core248d.lib
LIBS += C:/Users/Zamaster/Dropbox/Softwa~1/Mop!/dev/Libs/x64/opencv/build/x64/vc12/staticlib/opencv_features2d248d.lib
LIBS += C:/Users/Zamaster/Dropbox/Softwa~1/Mop!/dev/Libs/x64/opencv/build/x64/vc12/staticlib/opencv_highgui248d.lib
LIBS += C:/Users/Zamaster/Dropbox/Softwa~1/Mop!/dev/Libs/x64/opencv/build/x64/vc12/staticlib/opencv_imgproc248d.lib

Zamaster
1st April 2014, 21:38
Perfect! I clearly was strugglin' with the meaning of the linker options. This fixed my problem. Thanks.