Using OpenCV in QTCreator (linking problem) - opencv

I have a problem with the linking simpliest test program in QTCreator:
CODE:
#include <QtCore/QCoreApplication>
#include <cv.h>
#include <highgui.h>
#include <cxcore.hpp>
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat M(7,7,CV_32FC2,Scalar(1,3));
return 0;
}
.pro file:
QT -= gui
TARGET = testopencv
CONFIG += console
CONFIG -= app_bundle
INCLUDEPATH += C:/OpenCV2_1/include/opencv
TEMPLATE = app
LIBS += C:/OpenCV2_1/lib/cxcore210d.lib \
C:/OpenCV2_1/lib/cv210d.lib \
C:/OpenCV2_1/lib/highgui210d.lib\
C:/OpenCV2_1/lib/cvaux210d.lib
SOURCES += main.cpp
I've tried to use -L and -l like LIBS += -LC:/OpenCV2_1/lib -lcxcored
And in .pro file:
QMAKE_LIBDIR += C:/OpenCV2_1/lib/Debug
LIBS += -lcxcore210d \
-lcv210d \
-lhighgui210d
The errors are like:
debug/main.o:C:\griskin\test\app\testopencv/../../../../OpenCV2_1/include/opencv/cxcore.hpp:97: undefined reference to cv::format(char const*, ...)'
Could anyone help me? Thanks!
In Visual Studio it works but I need it works in QTCreator..

Qt uses the MinGW compiler and linker. It will happily link OpenCV .libs when you use the C interface. However due to ABI issues, it will not link C++ modules.
You will have to either restrict yourself to the C interfaces only (i.e. no cv::Mat), or get OpenCV to compile with MinGw (or compile Qt in/with VS). Also, see this thread.

This is what my .pro file looks like
INCLUDEPATH += C:\\opencv\\release\\install\\include\
LIBS += -LC:\\opencv\\release\\install\\bin \
-lopencv_core240 \
-lopencv_highgui240 \
-lopencv_imgproc240 \
-lopencv_features2d240 \
-lopencv_calib3d240 \
and replace
#include <cv.h>
with
#include <opencv/cv.h>
Above .pro file works perfect for me. I have used mingw to compile OpenCV and using mingw compiler tool chain in Qt.

It seems that QtCreator cannot locate lib files.
Try specifying the include file and libs as follows.
INCLUDEPATH += C:/OpenCV2_1/build/include/
LIBS += C:/OpenCV2_1/build/gpu/x86/lib/cxcore210d.lib
you should refer to build folder if you're using pre-build version of opencv.
(I recommend pre-build version if you're not that familiar with opencv)

Related

Can't run OpenCV-App because of runtime error missing Qt5OpenGL.dll and Qt5Test.dll

I want to build an application using the OpenCV library built with cmake.
First I downloaded the sources for opencv4.5.5. I created in that folder a subfolder mingwbuild and from there I can the command "cmake ../ -G "MinGW Makefiles"
Then I ran the command "mingw32-make install" and it took 2 hours to compile.
I then used cmake again to build a project, like described in the first answer here:
Configuring an c++ OpenCV project with Cmake
When I then ran the generated .exe, it would give me a runtime error "Qt5OpenGl.dll and Qt5Test.dll could not be found"
So I downloaded QtOpenGl.dll with MSYS2 from here https://packages.msys2.org/package/mingw-w64-x86_64-qt5-base
with the command "pacman -S mingw-w64-x86_64-qt5-base"
When I then run my opencvtest.exe , it says that it could not find the entry point of some method in QtOpenGl.dll . So the version or something must be wrong?
When searching for the package in the MSYS2 terminal, it says there are 10 packages I can download and install. The first four are:
$ pacman -Ss qt5-base
mingw32/mingw-w64-i686-qt5-base 5.15.3+kde+r174-2 (mingw-w64-i686-qt5)
A cross-platform application and UI framework (mingw-w64)
mingw32/mingw-w64-i686-qt5-base-debug 5.15.3+kde+r174-2 (mingw-w64-i686-qt5-debug)
A cross-platform application and UI framework (mingw-w64)
mingw64/mingw-w64-x86_64-qt5-base 5.15.3+kde+r174-2 (mingw-w64-x86_64-qt5) [installed]
A cross-platform application and UI framework (mingw-w64)
mingw64/mingw-w64-x86_64-qt5-base-debug 5.15.3+kde+r174-2 (mingw-w64-x86_64-qt5-debug)
Maybe if I choose the release package, I also have to set the option -DCMAKE_BUILD_TYPE=Release when invoking cmake?
Option x86 means 64-bit package and without x86, it means 32-bit packages. How can I figure out which of these I do need?
Any help is appreciated.
I finally managed to compile my OpenCVApp
The key was to work only in the MSYS2 MinGW x64 shell.
First I needed to install cmake like so:
pacman -S mingw-w64-x86_64-cmake
Then I had to install opencv with pacman like so:
pacman -S /mingw-w64-x86_64-opencv
The Qt5, I had to install like so:
pacman -S mingw-w64-x86_64-qt5-base
Then I created a new directory with src/main.cpp and CMakeLists.txt inside it like so:
Here CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
PROJECT (opencvtest)
find_package(OpenCV REQUIRED )
set( NAME_SRC
src/main.cpp
)
set( NAME_HEADERS
include/header.h
)
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( opencvtest ${NAME_SRC} ${NAME_HEADERS} )
target_link_libraries( opencvtest ${OpenCV_LIBS} )
Here main.cpp
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
const cv::Vec3b color_azure{ 255, 129, 0 };
const cv::Vec3b color_violet{ 255, 0, 127 };
const cv::Vec3b color_black {0,0,0};
int main() {
cout << "App started" << endl;
cv::Mat image = cv::Mat(cv::Size(800, 600), CV_8UC3, color_black);
std::vector<cv::Point> vs(5);
vs[0] = cv::Point(200, 450);
vs[1] = cv::Point(600, 250);
vs[2] = cv::Point(400, 50);
vs[3] = cv::Point(200, 250);
vs[4] = cv::Point(600, 450);
std::vector<int> nk(9);
nk[0] = 0;
nk[1] = 1;
nk[2] = 2;
nk[3] = 3;
nk[4] = 4;
nk[5] = 0;
nk[6] = 3;
nk[7] = 1;
nk[8] = 4;
std::stringstream ss("Das-ist-das-Haus-vom-Ni-ko-laus");
std::vector<std::string> silben(9);
int j = 0;
while (ss.good())
{
std::string substr;
getline(ss, substr, '-');
silben[j++] = substr + "-";
}
for (int i = 0; i < 8; i++) {
cv::line(image, vs[nk[i]], vs[nk[i + 1]], color_azure, 5);
cv::putText(image, silben[i], cv::Point(i*60, 475),
cv::FONT_HERSHEY_PLAIN, 1, color_violet, 2);
cv::imshow("Image", image);
cv::waitKey();
}
cv::imshow("Image", image);
cv::waitKey(0);
return 0;
}
Then compiling and running worked fine:
mkdir buildmingw
cd buildmingw
cmake .. -G "MinGW Makefiles"
mingw32-make
bin/opencvtest.exe

Need to pass parameters for library to compile simple program

This looks to be an easy one but I am not able to get it through.
So, I have installed OpenCV library and the executables are present at
/usr/local/opt/opencv/lib
apple#Apples-MacBook-Air.local:~/AGV/Vision$ ls /usr/local/opt/opencv/lib
cmake libopencv_fuzzy.dylib
libopencv_saliency.dylib
libade.a libopencv_gapi.4.5.3.dylib
libopencv_sfm.4.5.3.dylib
libcorrespondence.a libopencv_gapi.4.5.dylib
libopencv_sfm.4.5.dylib
....
....
And my $PATH variable looks like this:
apple#Apples-MacBook-Air.local:~/AGV/Vision$ echo $PATH
/usr/local/opt/opencv/lib:/usr/local/bin:/usr/local/opt/libxml2/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
If you notice, the path to installed libraries is part of the $PATH variable. Problem is that when I compile a simple program of OpenCV library, it says it can not find the file which seems frustrating.
Sample code:
#include <iostream>
#include <opencv2/highgui.hpp>
int main() {
}
Error I get is:
apple#Apples-MacBook-Air.local:~/AGV/Vision$ g++ agv.cpp
agv.cpp:2:10: fatal error: 'opencv2/highgui.hpp' file not found
#include <opencv2/highgui.hpp>
If I have added the library path to $PATH variable, shouldn't g++ take the libraries from that path and proceed?
What am I missing here?

PANIC: unprotected error in call to Lua API (undefined symbol: lua_gettop)

My environment is Lua-5.4.2 Luasocket-3.0-rc1.
When I run lua script directly, it work success.
When i run it through c language, it tell me error.
Error Msg is :
PANIC: unprotected error in call to Lua API (error running script: error loading module 'socket.core' from file '/usr/local/lib/lua/5.4/socket/core.so': undefined symbol: lua_gettop)
Aborted(core dumped)
Does anyone know why?
lua script code is:(test.lua)
#!/usr/local/bin/lua
local socket = require("socket")
print(socket._VERSION)
c code is:(main.c)
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int main(void)
{
lua_State *L;
L = luaL_newstate();
luaopen_base(L);
luaL_openlibs(L);
printf("lua enter\n");
if (luaL_dofile(L, "test.lua"))
{
luaL_error(L, "error running script: %s", lua_tostring(L, -1));
}
printf("lua exit\n");
while(1) pause();
lua_close(L);
return 0;
}
tl;dr: Pass -Wl,-E to GCC.
I was able to reproduce your problem with this Dockerfile:
FROM gcc:11.1.0
RUN wget https://www.lua.org/ftp/lua-5.4.2.tar.gz \
&& git clone https://github.com/diegonehab/luasocket.git
RUN tar zxf lua-5.4.2.tar.gz \
&& cd lua-5.4.2 \
&& make linux \
&& make install \
&& cd ../luasocket \
&& git checkout 5b18e475f38fcf28429b1cc4b17baee3b9793a62 \
&& make linux LUAV=5.4 \
&& make install LUAV=5.4
COPY test.lua main.c ./
When I run lua test.lua in the resulting Docker container, it works fine. When I run gcc -o test main.c /usr/local/lib/liblua.a -ldl -lm -Wl,-rpath='/usr/local/lib/lua/5.4/socket' && ./test, I get the same panic that you get.
The reason that the standalone Lua binary works and yours doesn't is that the former is linked with -E:
MYLDFLAGS= $(LOCAL) -Wl,-E
ld's documentation for -E says this about it:
If you use dlopen to load a dynamic object which needs to refer back to the symbols defined by the program, rather than some other dynamic object, then you will probably need to use this option when linking the program itself.
Lua uses dlopen to load C modules that you require, and said modules need to call Lua functions, which are linked into your binary, so it makes sense that you need this option. And indeed, when I add -Wl,-E to the GCC command line, then it works fine for me:
root#077bbb831441:/# gcc -o test main.c /usr/local/lib/liblua.a -ldl -lm -Wl,-rpath='/usr/local/lib/lua/5.4/socket' -Wl,-E && ./test
lua enter
LuaSocket 3.0-rc1
lua exit

pcap "undefined reference to" error

I'm trying to read in data from pcap files using pcap_open_offline(). I've used #include <pcap/pcap.h> and compiled with no errors after some debugging. Now I've come across another problem I can't seem to figure out. I wrote the following function:
void openPcap(char* filename){
printf("Opening file %s\n", filename);
pcap_t *pcap;
const unsigned char *packet;
char errbuf[PCAP_ERRBUF_SIZE];
struct pcap_pkthdr header;
pcap = pcap_open_offline(filename, errbuf);
if (pcap == NULL){
fprintf(stderr, "%s Malformed packet records in file %s",ER,filename);
}
}
And my pcap_open_offline call gives me about 100 of these errors when I try to compile:
pcap-linux.c:(.text+0xcd4): undefined reference to 'nl_handle_alloc'
pcap-linux.c:(.text+0xce8): undefined reference to 'genl_connect'
pcap-linux.c:(.text+0xcf6): undefined reference to 'genl_ctrl_alloc_cache'
pcap-linux.c:(.text+0xd0e): undefined reference to 'genl_ctrl_search_by_name'
pcap-linux.c:(.text+0xd64): undefined reference to 'nl_handle_destroy'
pcap-linux.c:(.text+0xdd7): undefined reference to 'nl_cache_free'
This is what my makefile looks like:
# -------------------------------
C=/afs/nd.edu/user14/csesoft/new/bin/gcc
CFLAGS=-Wall -std=c11 -I/afs/nd.edu/coursesp.18/cse/cse30341.01/support/gcc-libpcap/include -D_BSD_SOURCE
LD=/afs/nd.edu/user14/csesoft/new/bin/g++
#LD=g++
LDFLAGS=-lpthread
# # ----------------------------
LDFLAGS += -L/afs/nd.edu/coursesp.18/cse/cse30341.01/support/gcc-libpcap/lib -lpcap # Add your own flags here, or leave blank
threadedRE: threadedRE.o
$(LD) $^ $(LDFLAGS) -o $#
threadedRE.o: threadedRE.c
$(C) $(CFLAGS) -c $<
# C compiler
%.o: %.c
$(C) $(CFLAGS) -c $<
.PHONY: clean
clean:
rm -f threadedRE *.o
And my headers are:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <sys/stat.h>
#include <pcap/pcap.h>
Any suggestions would be greatly appreciated.
It isn't a compiling error, but a linking one. At the end of build process you should see something like "ld exited with error".
pcap_open_offline() seems to use nl_handle_alloc() and other functions, but linker can't find object files containing their implementation. Pointing linker to proper library which contains required object files by adding -lnl to LDFLAGS should do the trick.

Using Opencv 3.1 from CAFFE

I am trying to use OPENCV 3.1 from inside of the caffe . This is my test code
#include <caffe/caffe.hpp>
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#endif // USE_OPENCV
#include<iostream>
#ifdef USE_OPENCV
int main(){
std::cout<<"USE_OPENCV value is 1\n";
return 0;
}
#else
int main(){
std::cout<<"USE_OPENCV value is 0\n";
return 0;
}
#endif
I am compiling it using following command
g++ -I path_to_caffe/distribute/include/ test3.cpp -std=c++0x -lboost_system
It compiles with giving the following output while executing :
USE_OPENCV value is 0
Before compiling the caffe i make this changes in Makefile.config file:
USE_OPENCV := 1
. . .
OPENCV_VERSION := 3
. . .
USE_PKG_CONFIG := 1
While compiling caffe, I firstly cleand it and then compiled it using following commands:
make clean
make all -j $(($(nproc) + 1))
make test
make runtest
make pycaffe
make distribute
It compiles without giving error , but while testing my test file it does not gives the output that i expect.
All glories goes to Shai. The solution is to add -DUSE_OPENCV flag .
g++ -I path_to_caffe/distribute/include/ test3.cpp -std=c++0x -lboost_system -DUSE_OPENCV

Resources