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

Makefile for linking OpenCV and Existing Library (Without using cmake)

目前,我一直在研究如何使用 Makefile link 现有的 OpenCV 库。我还是 Makefile 的新手。我在 Internet 上搜索过,但大部分答案都在 CMake 上。即使有答案,结果的输出也包含错误。请查看我的 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

您有 linking 错误, 您需要 link 使用以下标志:

-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_video刺

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 *