用于链接OpenCV和现有库的Makefile(不使用cmake)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用于链接OpenCV和现有库的Makefile(不使用cmake)相关的知识,希望对你有一定的参考价值。

目前,我一直在研究如何使用Makefile将现有库与OpenCV链接。我还是Makefile的新手。我在互联网上搜索过,但主要是答案是在CMake上。即使有答案,结果的输出也包含错误。请看看我的Makefile,我做错了什么?

Makefile文件

###############################################################
#
# Purpose: Makefile for "head_tracking"
# Author.: robotis
# Version: 0.1
# License: GPL
#
###############################################################

TARGET = head_tracking

INCLUDE_DIRS = -I../../../include -I../../../../Framework/include

CXX = g++
CXXFLAGS += -O2 -DLINUX -Wall $(INCLUDE_DIRS)
#CXXFLAGS += -O2 -DDEBUG -DLINUX -Wall $(INCLUDE_DIRS)
LFLAGS += -lpthread -ljpeg -lrt
CPPFLAGS = $(shell pkg-config --cflags opencv2) #The one i added
LDLIBS = $(shell pkg-config --libs opencv2)     #The one i addded

OBJECTS =   main.o

all: $(TARGET)

clean:
    rm -f *.a *.o $(TARGET) core *~ *.so *.lo

libclean:
    make -C ../../../build clean

distclean: clean libclean

darwin.a:
    make -C ../../../build

$(TARGET): darwin.a $(OBJECTS)
    $(CXX) $(CFLAGS) $(OBJECTS) ../../../lib/darwin.a -o $(TARGET) $(LFLAGS)
    chmod 755 $(TARGET)

# useful to make a backup "make tgz"
tgz: clean
    mkdir -p backups
    tar czvf ./backups/head_tracking_`date +"%Y_%m_%d_%H.%M.%S"`.tgz --exclude backups *

Error Image: Undefined Refrenced

答案

您有链接错误,您需要链接以下标志:

-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab

OpenCV Undefined symbols for architecture x86_64: error

另一答案

好吧,我终于解决了我的链接错误。我现在可以用我的Robotis-Op库编译openCV。这是新的Makefile。

###############################################################
#
# Purpose: Makefile for "head_tracking"
# Author.: robotis
# Version: 0.1
# License: GPL
#
###############################################################

TARGET = head_tracking

INCLUDE_DIRS = -I../../../include -I../../../../Framework/include -I/usr/local/include/opencv2

CXX = g++
CXXFLAGS += -O2 -DLINUX -Wall $(INCLUDE_DIRS) `pkg-config --cflags opencv`
#CXXFLAGS += -O2 -DDEBUG -DLINUX -Wall $(INCLUDE_DIRS)
LFLAGS += -lpthread -ljpeg -lrt 
LDFLAGS = `pkg-config --libs opencv` 
#CPPFLAGS = $(shell pkg-config --cflags opencv2)
#LDLIBS = $(shell pkg-config --libs opencv2)

OBJECTS =   main.o

all: $(TARGET)

clean:
    rm -f *.a *.o $(TARGET) core *~ *.so *.lo

libclean:
    make -C ../../../build clean

distclean: clean libclean

darwin.a:
    make -C ../../../build

$(TARGET): darwin.a $(OBJECTS)
    $(CXX) $(CFLAGS) $(OBJECTS) ../../../lib/darwin.a -o $(TARGET) $(LFLAGS) $(LDFLAGS)
    chmod 755 $(TARGET)

# useful to make a backup "make tgz"
tgz: clean
    mkdir -p backups
    tar czvf ./backups/head_tracking_`date +"%Y_%m_%d_%H.%M.%S"`.tgz --exclude back

ups *

以上是关于用于链接OpenCV和现有库的Makefile(不使用cmake)的主要内容,如果未能解决你的问题,请参考以下文章

opencv静态链接库有啥用

OpenCV静态库编译与链接

将使用 ITK 或 OpenCV 库的现有 C++ 项目转换为 DLL 是不是可能?

在 Ubuntu 18.04 Jetson 中为 cv_bridge 链接 opencv 3.2

Makefile 链接静态库

使用 PGI 编译器链接到犰狳库的 Makefile