【发布时间】:2018-02-28 19:24:57
【问题描述】:
我使用 OpenCV 库在 Qt Creator 中制作控制台应用程序(我只是使用 QMake 和 IDE,而不是任何 Qt 库)。这是在 Ubuntu 上。
首先我动态地使用 OpenCV(使用 .so)但我想在没有安装 OpenCV 的 PC 上执行我的应用程序。所以我正在尝试制作一个独立的可执行文件:
我重新编译并静态安装OpenCV(使用参数
-DBUILD_SHARED_LIBS=OFF)我尝试在我的 project.pro 上添加
CONFIG += staticlib,但我注意到它仍在使用动态库我尝试在配置文件/调试/发布上进行编译,但这些构建选项都没有给我带来好的结果
我终于尝试添加
QMAKE_LFLAGS += -static并重新复制了opencv.pc上的行:
LIBS += -L/usr/local/lib -lopencv_contrib -lopencv_stitching -lopencv_nonfree -lopencv_superres -lopencv_ocl -lopencv_ts -lopencv_videostab -lopencv_gpu -lopencv_photo -lopencv_objdetect -lopencv_legacy -lopencv_video -lopencv_ml -lopencv_calib3d -lopencv_features_2d -lopencvhigh /local/share/OpenCV/3rdparty/lib -lIlmImf -ljasper -ltiff -lpng -ljpeg -lopencv_imgproc -lopencv_flann -lopencv_core -lzlib -lswscale-ffmpeg -lavutil-ffmpeg -lavformat-ffmpeg -lavcodec-ffmpeg -lgthread-2.0 -lfreetype -lfontconfig -lglib-2.0 -lgobject-2.0 -lpango-1.0 -lpangoft2-1.0 -lgio-2.0 -lgdk_pixbuf-2.0 -lcairo -latk-1.0 -lpangocairo-1.0 -lgdk-x11-2.0 -lgtk-x11-2.0 -lrt -lpthread -lm -ldl -lstdc++
我收到以下错误:
:-1: error: cannot find -ljasper
:-1: error: cannot find -ltiff
:-1: error: cannot find -ljpeg
:-1: error: cannot find -lgdk_pixbuf-2.0
编辑:
实际上我是为了研究目的而编程,我在图像处理和模式识别方面,一开始我在我的电脑上做一些小测试,但现在我必须在一个大型数据库上做测试,这是非常耗时的为什么我要使用集群,问题是集群没有opencv,这就是为什么我要带上我的.exe
为了简化事情,我制作了一个只有 main 和一些简单函数的小程序(其中显示了我在程序中使用的所有标题)
标题是:
#include "opencv2/core/core.hpp"
#include "math.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"
我将这一行放在我的 project.pro 中以识别最小依赖项:
LIBS += -L/usr/local/lib -lopencv_imgproc -lopencv_highgui -lopencv_core
这可以正常工作,但是当我添加参数时:
QMAKE_LFLAGS += -static
我有很多错误,例如:
grfmt_tiff.cpp:-1: error: undefined reference to `TIFFSetErrorHandler'
..
grfmt_jpeg.cpp:-1: error: undefined reference to `jpeg_start_decompress'
..
grfmt_exr.cpp:-1: error: undefined reference to `Imf::FrameBuffer::insert(char const*, Imf::Slice const&)'
..
grfmt_png.cpp:-1: error: undefined reference to `png_create_write_struct'
..
grfmt_jpeg2000.cpp:-1: error: undefined reference to `jas_cleanup'
..
system.cpp:-1: error: undefined reference to `pthread_spin_init'
persistence.cpp:-1: error: undefined reference to `gzeof'
rand.cpp:-1: error: undefined reference to `pthread_once'
..
(.text._ZNSt12_GLOBAL__N_13runEv+0x1d):-1: error: undefined reference to `pthread_setspecific'
安装完你告诉我要安装的东西
sudo apt-get install libjasper-dev libtiff-dev libjpeg-dev libgtk2.0-dev
并在我的 .pro 中添加这一行
LIBS += -L/usr/lib/x86_64-linux-gnu -ltiff -ljpeg -lpthread -lpng -ljasper -ljbig -lz -llzma
LIBS += -L/usr/local/share/OpenCV/3rdparty/lib -lIlmImf
我已经将问题缩小为 63 这个错误就像:
grfmt_png.cpp:-1: error: undefined reference to `png_set_longjmp_fn'
(.text+0x27a):-1: error: undefined reference to `deflateParams'
IlmThreadMutexPosix.cpp:-1: error: undefined reference to `pthread_mutex_destroy'
IlmThreadPosix.cpp:-1: error: undefined reference to `pthread_join'
ImfZipCompressor.cpp:-1: error: undefined reference to `compress'
(.text+0x185e):-1: error: undefined reference to `pthread_mutex_lock'
ImfPxr24Compressor.cpp:-1: error: undefined reference to `compress'
(.text._ZN12_GLOBAL__N_14pool4freeEPv.constprop.2+0x1d):-1: error: undefined reference to `pthread_mutex_lock'
这里是简化的代码源:
#include <iostream>
#include "opencv2/core/core.hpp"
#include "math.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"
#include <iostream>
#include <fstream>
using namespace std;
const double PI_2=atan (1.0)*2.0;
const double PI=atan (1.0)*4.0;
const double PI_4=atan (1.0);
const double PI3_4=PI_2+PI_4;
/*
void Paint_rect(){
cv::Mat imgq=cv::imread("/home/touka/Documents/PalmPrintProject/new-100-bmp-183-187/1_1.jpg",CV_LOAD_IMAGE_COLOR);
cv::Mat imgt=imgq.clone();
cv::rectangle(imgt,cv::Point (150,150),cv::Point (874,874),CV_RGB(255,0,0),2);
cv::imwrite("/home/touka/Desktop/test2.jpg",imgt);
}
void Save_Ort_Mat_File (cv::Mat mt, string file_name){
ofstream myfile;
myfile.open (file_name.c_str());
if(myfile){
for (int i=0;i<mt.rows;i++){
for (int j=0;j<mt.cols; j++)
myfile <<setprecision(8)<< ((mt.at<double>(i,j)*180)/PI)<<" | ";
myfile <<"\n" ;}
myfile.close();}
else{
cout <<"ERROR0";
}
}
*/
cv::Mat Sobel_Gradient_Image (cv::Mat img){
cv::Mat GX,GY,GXY;
cv::Point P(-1,-1);
cv::Vec3d X(1,2,1);
cv::Vec3d Y(1,0,-1);
cv::sepFilter2D(img,GX,img.depth(),Y,X,P,0,cv::BORDER_CONSTANT);
cv::sepFilter2D(img,GY,img.depth(),X,Y,P,0,cv::BORDER_CONSTANT);
vector<cv::Mat> m;
m.push_back(GX); m.push_back(GY);
cv::merge(m,GXY);
return GXY;
}
int main(int argc, char *argv[])
{
cv::Mat test;
cv::Mat imgq=cv::imread("/home/touka/Documents/PalmPrintProject/new-100-bmp-183-187/1_1.jpg",CV_LOAD_IMAGE_COLOR);
// test= Sobel_Gradient_Image(imgq);
// Paint_rect();
// Save_Ort_Mat_File(test,"file");
cout << "Hello World!" << endl;
return 0;
}
编辑 在更改库顺序并添加 -pthread 后,我只重复了一个错误 3 次:
png.cpp:-1: error: undefined reference to `png_set_longjmp_fn'
不知道为什么这个错误会重复3次这里是新的顺序:
LIBS += -L/usr/local/lib -lopencv_imgproc -lopencv_highgui -lopencv_core
LIBS += -L/usr/local/share/OpenCV/3rdparty/lib -lIlmImf
LIBS += -L/usr/lib/x86_64-linux-gnu -lpng -ljasper -ltiff -ljpeg -lpthread -ljbig -llzma -pthread -lz
【问题讨论】:
标签: c++ opencv qt-creator qmake static-linking