Ask Your Question
1

Why my opencv program failed to compile with static lib

asked 2017-09-21 03:26:26 -0600

cyhaolihai gravatar image

updated 2017-09-21 04:09:08 -0600

berak gravatar image

I intend to use opencv2.4.11 static library for compilation, I have cross-compiled to generate a static library and dynamic library. But when I used static lib to compile my test program in the ubuntu operation, there happened errors (there was no problem with the dynamic library ), the problem was as follows:

chenyao@chenyao-OptiPlex-3020:~/code/test-arm$ make all
/opt/freescale/usr/local/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-g++   -c -I/opt/arm/arm-opencv2.4.11/include                               test.cpp -O3 -Wno-deprecated      
In file included from /opt/arm/arm-opencv2.4.11/include/opencv2/flann/kmeans_index.h:50:0,
                 from /opt/arm/arm-opencv2.4.11/include/opencv2/flann/all_indices.h:38,
                 from /opt/arm/arm-opencv2.4.11/include/opencv2/flann/flann_base.hpp:44,
                 from /opt/arm/arm-opencv2.4.11/include/opencv2/flann/flann.hpp:50,
                 from /opt/arm/arm-opencv2.4.11/include/opencv/cv.h:69,
                 from test.cpp:3:
/opt/arm/arm-opencv2.4.11/include/opencv2/flann/logger.h:73:9: note: the mangling of 'va_list' has changed in GCC 4.4
/opt/freescale/usr/local/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-g++   -L/opt/arm/arm-opencv2.4.11/lib                                   -lopencv_core -lopencv_imgproc -lopencv_highgui -lpthread -lrt -ldl -o  test test.o -O3 -Wno-deprecated    
test.o: In function `cv::Mat::~Mat()':
test.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x78): undefined reference to `cv::fastFree(void*)'
test.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x88): undefined reference to `cv::Mat::deallocate()'
test.o: In function `main':
test.cpp:(.text.startup+0x24): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
test.cpp:(.text.startup+0x94): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test.cpp:(.text.startup+0xa0): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
test.cpp:(.text.startup+0xb0): undefined reference to `cv::waitKey(int)'
collect2: ld returned 1 exit status
make: *** [test] fault 1

The next text is my Makefile:

APPNAME  = test
#INCLUDES = -I/home/chenyao/opencv-arm/opencv-2.4.11/build/install/include  
INCLUDES = -I/opt/arm/arm-opencv2.4.11/include                              
#LIBDIRS  = -L/home/chenyao/opencv-arm/opencv-2.4.11/build/install/lib      #dynamic lib
LIBDIRS  = -L/opt/arm/arm-opencv2.4.11/lib                   ************************#static lib

LIBS     = -lopencv_core -lopencv_imgproc -lopencv_highgui -lpthread -lrt -ldl

OPT      = -O3 -Wno-deprecated    
CC       = /opt/freescale/usr/local/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-g++  

.PHONY: all clean  

OBJS     = $(APPNAME).o  

clean:  
    rm -f *.o *~ $(APPNAME)  

all:$(APPNAME)  
    echo all:make complete  

%.o:%.cpp  
    $(CC) -c $(INCLUDES) $+ $(OPT)  

$(APPNAME):$(OBJS)  
    $(CC) $(LIBDIRS) $(LIBS) *A* -o *B* $@$+ *C* $(OPT) *D*

The fifth line is the location of my static library. These A\B\C\D in the bottom of text are where I have inserted "-static" . And when I used the dynamic library compiler, it is possible to generate executable files.

I would like to ask you why my program occurred errors when I used a static library compiler(dynamic library is possible)?

[edit] new problems showing up:

/opt/freescale/usr/local/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain ...
(more)
edit retag flag offensive close merge delete

Comments

1

Sorry, this is my first time to ask question in this website. I have replaced those pictures with text.

cyhaolihai gravatar imagecyhaolihai ( 2017-09-21 03:39:28 -0600 )edit

nvm, thanks thanks a lot for complying ! now it can be properly indexed for search, etc.

berak gravatar imageberak ( 2017-09-21 03:50:00 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2017-09-21 03:48:52 -0600

berak gravatar image

updated 2017-09-21 04:21:31 -0600

with static linking (and gcc), the ORDER of the libs (and object files) matters.

since highgui depends on core, it must be:

LIBS     =  -lopencv_highgui -lopencv_imgproc -lopencv_core -lpthread -lrt -ldl

in the same manner, your test.o has to go before the libs:

$(APPNAME):$(OBJS)  
    $(CC)  $+ $(LIBDIRS) $(LIBS) -o  $@ $(OPT)

not sure, if i got the last line right, but i hope, you get the idea:

with static linking,  you have to sort your dependancies, bottom up.

[edit] more libs missing: (jpeg,tiff,png,zlib)

last time i tried with 2.4 (no more using that):

g++ -std=c++0x cv.cpp -I ocv/include -L ocv/lib -lopencv_highgui -lopencv_calib3d -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lopencv_legacy -lopencv_imgproc -lopencv_core -ljpeg -lpng -ltiff -lrt -lz -lpthread -o cv

(ofc, you will have to adjust library/include paths)

one last thing about jpg/tiff/png: if you use the system libs, it is -ljpeg , if you use opencv's 3rdparty ones(i think, that's what you should do with cross-compiling) it is: -llibjpeg

edit flag offensive delete link more

Comments

1

I have tried your solution, but there have more faults. Please look at my answer(there have too many words, so I cannot put those faults at there)

cyhaolihai gravatar imagecyhaolihai ( 2017-09-21 03:56:57 -0600 )edit

And if I put "$+" in the original location("$@$+"), the errors became to just like the next:

chenyao@chenyao-OptiPlex-3020:~/code/test-arm$ make all /opt/freescale/usr/local/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-g++ -L/opt/arm/arm-opencv2.4.11/lib -lopencv_highgui -lopencv_imgproc -lopencv_core -lpthread -lrt -ldl -o testtest.o -O3 -Wno-deprecated -static /opt/freescale/usr/local/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/../arm-fsl-linux-gnueabi/multi-libs/default/usr/lib/crt1.o: In function _start': init.c:(.text+0x34): undefined reference tomain' collect2: ld returned 1 exit status make: * [test] 错误 1

cy941220 gravatar imagecy941220 ( 2017-09-21 04:09:56 -0600 )edit

sure. and i put the update into your question.

berak gravatar imageberak ( 2017-09-21 04:11:01 -0600 )edit

yea, there's simply more libs missing.

berak gravatar imageberak ( 2017-09-21 04:12:36 -0600 )edit

Why computer tell me: undefined reference to 'main' ?????? I wanna to crazy, please help me. The static compile is tooooooooooooo troublesome.

cy941220 gravatar imagecy941220 ( 2017-09-21 04:16:32 -0600 )edit

hehe, i know, it's totally annoying, but there's just a space missing here:

-o testtest.o

-o test test.o
berak gravatar imageberak ( 2017-09-21 04:26:04 -0600 )edit

are you Chinese?because I saw you said "hehe"

cy941220 gravatar imagecy941220 ( 2017-09-21 04:56:09 -0600 )edit
1

I have solved this question:

arm-fsl-linux-gnueabi-g++ -std=c++0x test.cpp -I /opt/arm/arm-opencv2.4.11/include -L /opt/arm/arm-opencv2.4.11/lib -L /opt/arm/arm-opencv2.4.11/share/OpenCV/3rdparty/lib -lopencv_highgui -lopencv_calib3d -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lopencv_legacy -lopencv_imgproc -lopencv_core -llibjpeg -llibpng -llibtiff -lzlib -lpthread -lrt -ldl -o test Thank you very much!!!!!!!!!!!!!!!!!

cy941220 gravatar imagecy941220 ( 2017-09-21 05:24:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-09-21 03:26:26 -0600

Seen: 2,502 times

Last updated: Sep 21 '17