Sky
Sky

Reputation: 23

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

Currently, i am been working on how to link an existing library with OpenCV using Makefile. I am still new to Makefile. I had googled on the Internet but mostly the answer is on CMake. Even there is an answer, the output of the result contain errors. Please have a look on my Makefile, am i doing anything wrong?

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

Upvotes: 0

Views: 4370

Answers (2)

Sky
Sky

Reputation: 23

Okay i finally solve my linking error. I can now compile openCV with my Robotis-Op library. This is the new 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 *

Upvotes: 1

user3808268
user3808268

Reputation: 36

You are having the linking errors, You need to link with the following flags:

-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

Upvotes: 0

Related Questions