Reputation: 315
I am quite new to c++ Makefile. Recently, I am trying to re-implement the dense trajectory algorithm introduced by this paper: https://lear.inrialpes.fr/people/wang/dense_trajectories
However, I was struggling with Makefile that they provided for quite a while. The Makefile is as follows:
# set the binaries that have to be built
TARGETS := DenseTrack Video
# set the build configuration set
BUILD := release
#BUILD := debug
# set bin and build dirs
BUILDDIR := .build_$(BUILD)
BINDIR := $(BUILD)
# libraries
LDLIBS = $(addprefix -l, $(LIBS) $(LIBS_$(notdir $*)))
LIBS := \
opencv_core opencv_highgui opencv_video opencv_imgproc \
avformat avdevice avutil avcodec swscale
# set some flags and compiler/linker specific commands
CXXFLAGS = -pipe -D __STDC_CONSTANT_MACROS -D STD=std -Wall $(CXXFLAGS_$(BUILD)) -I. -I/usr/local/include -I/home/wei/ffmpeg_build/include
CXXFLAGS_debug := -ggdb
CXXFLAGS_release := -O3 -DNDEBUG -ggdb
LDFLAGS = -L/usr/local/lib -L/home/wei/ffmpeg_build/lib -pipe -Wall $(LDFLAGS_$(BUILD))
LDFLAGS_debug := -ggdb
LDFLAGS_release := -O3 -ggdb
include make/generic.mk
I can verify that my opencv2.4.2 and ffmpeg5.4.0 are successfully installed in Ubuntu 16.04, by running an example using those two libraries.
For opencv: The lib path is: /usr/local/lib
libavcodec.a libopencv_gpu.so.2.4
libopencv_photo.so.2.4
libavdevice.a libopencv_gpu.so.2.4.2 libopencv_photo.so.2.4.2
libavfilter.a libopencv_highgui.so libopencv_stitching.so
libavformat.a libopencv_highgui.so.2.4 libopencv_stitching.so.2.4
libavutil.a libopencv_highgui.so.2.4.2 libopencv_stitching.so.2.4.2
libopencv_calib3d.so libopencv_imgproc.so libopencv_ts.so
libopencv_calib3d.so.2.4 libopencv_imgproc.so.2.4 libopencv_ts.so.2.4
libopencv_calib3d.so.2.4.2 libopencv_imgproc.so.2.4.2 libopencv_ts.so.2.4.2
libopencv_contrib.so libopencv_legacy.so libopencv_video.so
libopencv_contrib.so.2.4 libopencv_legacy.so.2.4 libopencv_video.so.2.4
libopencv_contrib.so.2.4.2 libopencv_legacy.so.2.4.2 libopencv_video.so.2.4.2
libopencv_core.so libopencv_ml.so libopencv_videostab.so
libopencv_core.so.2.4 libopencv_ml.so.2.4 libopencv_videostab.so.2.4
libopencv_core.so.2.4.2 libopencv_ml.so.2.4.2 libopencv_videostab.so.2.4.2
libopencv_features2d.so libopencv_nonfree.so libpostproc.a
libopencv_features2d.so.2.4 libopencv_nonfree.so.2.4 libswresample.a
libopencv_features2d.so.2.4.2 libopencv_nonfree.so.2.4.2 libswscale.a
libopencv_flann.so libopencv_objdetect.so pkgconfig
libopencv_flann.so.2.4 libopencv_objdetect.so.2.4 python2.7
libopencv_flann.so.2.4.2 libopencv_objdetect.so.2.4.2 python3.5
libopencv_gpu.so libopencv_photo.so
The include path is: usr/local/include:
libavcodec libavfilter libavutil libswresample opencv
libavdevice libavformat libpostproc libswscale opencv2
For ffmpeg: The lib path is: /home/user/ffmpeg_build/lib
libavcodec.a libavfilter.a libavutil.a libfdk-aac.la libswresample.a libx265.a
libavdevice.a libavformat.a libfdk-aac.a libpostproc.a libswscale.a pkgconfig
The include path is: /home/user/ffmpeg/include
fdk-aac libavdevice libavformat libpostproc libswscale x265.h
libavcodec libavfilter libavutil libswresample x265_config.h
So when I make it with Makefile, I did not get error but it seems linking is working properly, nor is the compiled output.
I tried to directly compile with g++ command.
sudo g++ -o Video -pipe -D __STDC_CONSTANT_MACROS -D STD=std -Wall -I. -I/usr/local/include -O3 -DNDEBUG -ggdb -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc -lavformat -lavdevice -lavutil -lavcodec -lswscale *.h Video.cpp
However,it returns errors as:
/tmp/ccjYmwI4.o: In function `main':
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:33: undefined reference to `cvCreateFileCapture'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:48: undefined reference to `cvQueryFrame'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:57: undefined reference to `cvCopy'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:60: undefined reference to `cvShowImage'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:61: undefined reference to `cvWaitKey'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:53: undefined reference to `cvCreateImage'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:70: undefined reference to `cvDestroyWindow'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:41: undefined reference to `cvNamedWindow'
collect2: error: ld returned 1 exit status
I have been working on this issue for several days. Any help or suggestions would be really appreciated.
Thanks
Upvotes: 1
Views: 317
Reputation: 9629
The problem comes from your linker call:
The library options (-l
) must come after the code to compile or to link:
Correct:
# code in foo.cpp need function in libmylib.so
g++ foo.cpp -lmylib
# code in foo.o need function in libmylib.so
g++ foo.o -lmylib
Incorrect:
# code in foo.cpp need function in libmylib.so
g++ -lmylib foo.cpp
# code in foo.o need function in libmylib.so
g++ -lmylib foo.o
Upvotes: 1