makefile 编写(一)makfile调用opencv静态库

依赖opencv的makefile

CFLAGS= -g -fPIE -ldl -o

SOURCES=test.cpp
TARGET_NAME=demo

$(TARGET_NAME):
	@echo Compling begin
	@g++ $(CFLAGS) ./$(TARGET_NAME)  $(SOURCES) -L../lib -lPredict -lopencv_core -lopencv_highgui -lopencv_imgproc -llibpng -llibtiff -llibjpeg -llibjasper -lIlmImf -lpthread -ldl -lzlib -lrt -Wl,-rpath,../lib -I../include -I../include/opencv -I../include/opencv2
	@echo Compling end
		
.PHONY: clean
clean:
	@echo Cleaning begin
	@$-rm -f $(TARGET_NAME)
	@echo Cleaning end

多参考几个文件整理一下,目前找到的写的都不是很清晰。

参考:[makefile]一个完整的makefile

参考:Makefile经典教程(一个很棒很清晰的讲解)

 

#include <stdio.h>
#include "opencv2/opencv.hpp"

int main()
{

    cv::Mat image = cv::imread("./testPic/jianhui.jpg");

    //cv::imshow("xxx", image);

    //cv::waitKey(40);

    printf("image.col=%d image.row=%d \n", image.cols, image.rows);

    cv::imwrite("aa.jpg", image);

    return 0;
}

Makefile


EXE=main

#SUBDIR=src object
#CXXFLAGS:编译选项, LDFLAGS:链接选项
thirdparty_root := ../opencv4.2lib/static
CC := g++


#export PKG_CONFIG_PATH := ${thirdparty_root}/lib/pkgconfig:${PKG_CONFIG_PATH}
#INCS += `pkg-config --cflags opencv`
#LIBS += `pkg-config --libs opencv`

INCS := -I${thirdparty_root}/include


LIBS += -L${thirdparty_root}/lib -lopencv_imgcodecs -lopencv_dnn -lopencv_imgproc -lopencv_core 
#LIBS += -L${thirdparty_root}/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core # 亲们,顺序不能变,因为这个快把我整崩溃了
LIBS += -L${thirdparty_root}/share/OpenCV/3rdparty/lib -lIlmImf -llibjasper -llibjpeg-turbo  -llibpng -llibtiff -llibwebp -lzlib -ldl



CXXFLAGS := -Wall -c -O2 -std=c++11 $(INCS)
LDFLAGS := $(LIBS)
LDFLAGS += -lpthread
#LDFLAGS += -Wl,-rpath,${thirdparty_root}/share/OpenCV/3rdparty/lib:${thirdparty_root}/lib: # 动态库的时候需要寻找的路径



#CXX_SOURCES =$(foreach dir,$(SUBDIR), $(wildcard $(dir)/*.cpp))
#CXX_OBJECTS=$(patsubst  %.cpp, %.o, $(CXX_SOURCES))

CXX_OBJECTS := $(patsubst %.cpp,%.o,$(shell find . -name "*.cpp"))
DEP_FILES  =$(patsubst  %.o,  %.d, $(CXX_OBJECTS))

$(EXE): $(CXX_OBJECTS)
	$(CC)  $(CXX_OBJECTS) -o $(EXE) $(LDFLAGS)
	
%.o: %.cpp
	$(CC)  -c  $(CXXFLAGS) -MMD $<  -o  $@

-include $(DEP_FILES)

clean: 
	rm  -rf  $(CXX_OBJECTS)  $(DEP_FILES)  $(EXE)

test:
	echo $(CXX_OBJECTS)

上面的Makefile 是用调用动态库的方式来调用静态库,如果同时存在静态库和动态库,会优先调用动态库



EXE=test

#SUBDIR=src object
#CXXFLAGS:编译选项, LDFLAGS:链接选项
CC := g++
thirdparty_root := ../opencv4.2lib/static


#INCS += `pkg-config --cflags opencv`
#INCS += -I/usr/lib/jdk1.8.0_191/include
#INCS += -I/usr/lib/jdk1.8.0_191/include/linux/

INCS += -I./include

INCS := -I${thirdparty_root}/include

# 这种方式调用静态库,保证在在动态库也存在的时候也能调用到静态库
LIBS += ${thirdparty_root}/lib/libopencv_imgcodecs.a
LIBS += ${thirdparty_root}/lib/libopencv_dnn.a
LIBS += ${thirdparty_root}/lib/libopencv_imgproc.a
LIBS += ${thirdparty_root}/lib/libopencv_core.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/libIlmImf.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/liblibjasper.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/liblibjpeg-turbo.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/liblibpng.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/liblibtiff.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/liblibwebp.a
LIBS += ${thirdparty_root}/share/OpenCV/3rdparty/lib/libzlib.a

LIBS += -ldl
#LIBS += `pkg-config --libs opencv`


CXXFLAGS := -Wall -c  -O2 -std=c++11 $(INCS)
LDFLAGS := $(LIBS) 
LDFLAGS += -lpthread

#CXXFLAGS += -I/usr/local/include/
#LDFLAGS += -L/usr/local/lib -lopencv_world

#CXX_SOURCES =$(foreach dir,$(SUBDIR), $(wildcard $(dir)/*.cpp))
#CXX_OBJECTS=$(patsubst  %.cpp, %.o, $(CXX_SOURCES))

CXX_OBJECTS := $(patsubst %.cpp,%.o,$(shell find . -name "*.cpp"))
CXX_OBJECTS += $(patsubst %.c,%.o,$(shell find . -name "*.c"))

DEP_FILES  =$(patsubst  %.o,  %.d, $(CXX_OBJECTS))

$(EXE): $(CXX_OBJECTS)
	$(CC)  $(CXX_OBJECTS) -o $(EXE) $(LDFLAGS)

%.o: %.cpp
	$(CC)  -c  $(CXXFLAGS) -MMD $<  -o  $@

-include $(DEP_FILES)

clean: 
	rm  -rf  $(CXX_OBJECTS)  $(DEP_FILES)  $(EXE) 

OBJ_DIR = obj
move:
	if [ ! -d $(OBJ_DIR) ]; then mkdir -p $(OBJ_DIR); fi;\
	mv $(CXX_OBJECTS) $(OBJ_DIR); \
	mv $(DEP_FILES) $(OBJ_DIR); \
	mv  $(EXE) $(OBJ_DIR)
	
test:
	echo $(CXX_OBJECTS)

 

参考:Makefile 如何调用opencv 的静态库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落花逐流水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值