linux makefile添加opencv库

下面是已有的makefile的部分代码

PREFIX = /usr/local
INCLUDES = -I../UsageEnvironment/include -I../groupsock/include -I../liveMedia/include -I../BasicUsageEnvironment/include
# Default library filename suffixes for each library that we link with. The "config.*" file might redefine these later.
libliveMedia_LIB_SUFFIX = $(LIB_SUFFIX)
libBasicUsageEnvironment_LIB_SUFFIX = $(LIB_SUFFIX)
libUsageEnvironment_LIB_SUFFIX = $(LIB_SUFFIX)
libgroupsock_LIB_SUFFIX = $(LIB_SUFFIX)

##### Change the following for your environment:
COMPILE_OPTS = $(INCLUDES) -m64 -fPIC -I/usr/local/include -I. -O2 -DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64
MEDIA_SERVER = live555MediaServer$(EXE)
ALL = $(MEDIA_SERVER)
all: $(ALL)
MEDIA_SERVER_OBJS = live555MediaServer.$(OBJ) DynamicRTSPServer.$(OBJ)
live555MediaServer.$(CPP): DynamicRTSPServer.hh version.hh
DynamicRTSPServer.$(CPP): DynamicRTSPServer.hh
USAGE_ENVIRONMENT_DIR
= ../UsageEnvironment USAGE_ENVIRONMENT_LIB = $(USAGE_ENVIRONMENT_DIR)/libUsageEnvironment.$(libUsageEnvironment_LIB_SUFFIX) BASIC_USAGE_ENVIRONMENT_DIR = ../BasicUsageEnvironment BASIC_USAGE_ENVIRONMENT_LIB = $(BASIC_USAGE_ENVIRONMENT_DIR)/libBasicUsageEnvironment.$(libBasicUsageEnvironment_LIB_SUFFIX) LIVEMEDIA_DIR = ../liveMedia LIVEMEDIA_LIB = $(LIVEMEDIA_DIR)/libliveMedia.$(libliveMedia_LIB_SUFFIX) GROUPSOCK_DIR = ../groupsock GROUPSOCK_LIB = $(GROUPSOCK_DIR)/libgroupsock.$(libgroupsock_LIB_SUFFIX) LOCAL_LIBS = $(LIVEMEDIA_LIB) $(GROUPSOCK_LIB) $(BASIC_USAGE_ENVIRONMENT_LIB) $(USAGE_ENVIRONMENT_LIB) LIBS = $(LOCAL_LIBS) $(LIBS_FOR_CONSOLE_APPLICATION) live555MediaServer$(EXE): $(MEDIA_SERVER_OBJS) $(LOCAL_LIBS) $(LINK)$@ $(CONSOLE_LINK_OPTS) $(MEDIA_SERVER_OBJS) $(LIBS) #live555MediaServer$(EXE): $(MEDIA_SERVER_OBJS) $(LOCAL_LIBS) # c++ -o $@ -L. $(MEDIA_SERVER_OBJS) $(LIBS)

首先我们需要看main程序包含了哪些第三方头文件,打开main程序

linux makefile添加opencv库

可以看到包含了core.hpp  imgproc.hpp  highgui.hpp videoio.hpp 这四个opencv库的头文件,因此首先需要找到这些头文件的位置,将这些头文件位置写入到makefile中。

可以通过文件搜索来找到这些文件的位置

linux makefile添加opencv库

可以看到这些文件都在这个文件夹中,由于头文件中自带了这些文件的上级文件夹名,因此添加路径时也可以只包括到这个文件夹的上级文件夹名称,即opencv4即可

所以在INCLUDE后面添加 -I/usr/local/include/opencv4

头文件加进去之后,还要找到对应的库添加到makefile中,库的名称通常和头文件名称对应,如果不知道库的名称,可以通过名称搜索来寻找这些库的位置,比如core.hpp可以搜索core.a 或者 core.so,然后在搜索出来的结果中筛选出正确的库文件

linux makefile添加opencv库

这里的libopencv_core.so就是需要添加的库文件之一了。

进入到这个路径的文件夹中,可以看到这个文件夹中还包含有其他的头文件对应的库,因此是这个路径没错了

linux makefile添加opencv库

然后就需要在makefile中添加

OPENCV_DIR = -L/usr/local/lib
OPENCV_LIBS= $(OPENCV_DIR) -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_videoio

OPENCV_LIBS 这个变量就是需要添加的库路径和库

然后再把这个库添加到编译规则中去

将LIBS中添加 $(OPENCV_LIBS)  这样在执行编译规则时,才能用上这些库,一定要添加到规则中,让规则找得到这些库才算添加成功

修改之后的makefile如下所示,修改过的部分用红色标记出

PREFIX = /usr/local
INCLUDES = -I../UsageEnvironment/include -I../groupsock/include -I../liveMedia/include -I../BasicUsageEnvironment/include  -I/usr/local/include/opencv4
# Default library filename suffixes for each library that we link with. The "config.*" file might redefine these later.
libliveMedia_LIB_SUFFIX = $(LIB_SUFFIX)
libBasicUsageEnvironment_LIB_SUFFIX = $(LIB_SUFFIX)
libUsageEnvironment_LIB_SUFFIX = $(LIB_SUFFIX)
libgroupsock_LIB_SUFFIX = $(LIB_SUFFIX)

##### Change the following for your environment:
COMPILE_OPTS = $(INCLUDES) -m64 -fPIC -I/usr/local/include -I. -O2 -DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64
MEDIA_SERVER = live555MediaServer$(EXE)
ALL = $(MEDIA_SERVER)
all: $(ALL)
MEDIA_SERVER_OBJS = live555MediaServer.$(OBJ) DynamicRTSPServer.$(OBJ)
live555MediaServer.$(CPP): DynamicRTSPServer.hh version.hh
DynamicRTSPServer.$(CPP): DynamicRTSPServer.hh

USAGE_ENVIRONMENT_DIR = ../UsageEnvironment
USAGE_ENVIRONMENT_LIB = $(USAGE_ENVIRONMENT_DIR)/libUsageEnvironment.$(libUsageEnvironment_LIB_SUFFIX)
BASIC_USAGE_ENVIRONMENT_DIR = ../BasicUsageEnvironment
BASIC_USAGE_ENVIRONMENT_LIB = $(BASIC_USAGE_ENVIRONMENT_DIR)/libBasicUsageEnvironment.$(libBasicUsageEnvironment_LIB_SUFFIX)
LIVEMEDIA_DIR = ../liveMedia
LIVEMEDIA_LIB = $(LIVEMEDIA_DIR)/libliveMedia.$(libliveMedia_LIB_SUFFIX)
GROUPSOCK_DIR = ../groupsock
GROUPSOCK_LIB = $(GROUPSOCK_DIR)/libgroupsock.$(libgroupsock_LIB_SUFFIX)
OPENCV_DIR = -L/usr/local/lib
OPENCV_LIBS= $(OPENCV_DIR) -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_videoio

LOCAL_LIBS = $(LIVEMEDIA_LIB) $(GROUPSOCK_LIB) $(BASIC_USAGE_ENVIRONMENT_LIB) $(USAGE_ENVIRONMENT_LIB)
LIBS = $(LOCAL_LIBS) $(OPENCV_LIBS) $(LIBS_FOR_CONSOLE_APPLICATION)

live555MediaServer$(EXE): $(MEDIA_SERVER_OBJS) $(LOCAL_LIBS)
$(LINK)$@ $(CONSOLE_LINK_OPTS) $(MEDIA_SERVER_OBJS) $(LIBS)

#live555MediaServer$(EXE): $(MEDIA_SERVER_OBJS) $(LOCAL_LIBS)
# c++ -o $@ -L. $(MEDIA_SERVER_OBJS) $(LIBS)

添加完成之后,在执行make,就可以看到不再提示错误

linux makefile添加opencv库

或者提示找不到函数的错误

会正常完成编译。

linux makefile添加opencv库

上一篇:eclipse-source not found


下一篇:Linux系统安装部署