一直以来使用opencv都是用cmake编译,然而cmake需要先cmake再make才能生成可执行文件,平时写小demo的时候感觉特麻烦,所有想写个Makefile直接make,Makefile文件长这样:
CC = g++
SRCS = helloword.cpp
PROG = helloword
OPENCV = `pkg-config opencv --cflags --libs`
LIBS = $(OPENCV)
$(PROG):$(SRCS)
$(CC) -o $(PROG) $(SRCS) $(LIBS) -std=c++11
执行make命令的时候看到实际执行的命令是:
g++ `pkg-config --cflags opencv` `pkg-config --libs opencv` -o helloword helloword.cpp --std=c++11
然而出现了错误:
ld: library not found for -lippicv
clang: error: linker command failed with exit code 1 (use -v to see invocation)
使用cmake是可以正常编译运行的,说明opencv环境应该没问题,一再确认Makefile写法应该也没问题。报错意思貌似是ippicv这个library没找到,有可能是pkg-config的锅,google之,果然,,,问题在于ippicv这个库是个第三方的库,opencv的pkg-config文件opencv.pc只说要链接这个库,然而并没有给定这个库的链接,修改下这个文件就可以了,这个文件一般都是在opencv安装目录/lib/pkg-config/
下,例如我的在
/usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc
修改这个文件为:
# Package Information for pkg-config
prefix=/usr/local/Cellar/opencv3/3.1.0_3
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
libdir3rd=${exec_prefix}/share/OpenCV/3rdparty/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.1.0
Libs: -L${exec_prefix}/lib -L${libdir3rd} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lippicv -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
Libs.private: -framework OpenCL -L/usr/local/Cellar/qt/4.8.7_2/lib -lQtGui -lQtTest -lQtCore -L/usr/local/opt/jpeg/lib -ljpeg -L/usr/local/lib -lwebp -lpng -ltiff -lImath -lIlmImf -lIex -lHalf -lIlmThread -L/usr/lib -lz -framework QTKit -framework QuartzCore -framework AppKit
Cflags: -I${includedir_old} -I${includedir_new}
搞定,收工!
网友评论