Ask Your Question

Compile static application

asked 2019-05-05 09:25:44 -0500

I want to make my openCV application portable somehow. So far i have compiled opencv with the BUILD_SHARED_LIBS=OFF command successfully, and the resulting folder containes the precious ".a" files. when i compile my application with the following command:

g++ -o atafut -std=c++11 -I /home/moritz/opencv/opencv-4.1.0-built/include/opencv4 --static -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs -lopencv_videoio -O0 -g -pthread main.cpp

i get the errors

/usr/bin/ld: cannot find -lopencv_core

/usr/bin/ld: cannot find -lopencv_imgproc

/usr/bin/ld: cannot find -lopencv_highgui

/usr/bin/ld: cannot find -lopencv_imgcodecs

/usr/bin/ld: cannot find -lopencv_videoio

collect2: error: ld returned 1 exit status

when i leave out the --static flag, everything works fine, but my application is not portable.

I am a bit of a noob, so hopefully i am forgetting something obvious.

edit retag flag offensive close merge delete


when i leave out the --static flag, everything works fine

i find it hard to believe that, your library list is far from complete (all actual img codecs, zlib missing)

DID you build dynamic libs before ? (if not so, --static should have no effect at all)

berak gravatar imageberak ( 2019-05-06 04:42:51 -0500 )edit

Ok, i have gotten a bit further since. The reason it worked without static was that i also had the opencv package installed on my system that was dynamically compiled and therefore no use for this issue. I uninstalled this package and i always got the same error message with and without static. Next i added -L /home/moritz/opencv/opencv-4.1.0-built/lib64to the compile command and i also added this directory to my LD_LIBRARY_PATH variable. This changed my error message, YAY! The new error message of the gcc is huge and can be found here: it basically complains about a huge number of undefined references any ideas how to proceed?

Moritz10071996 gravatar imageMoritz10071996 ( 2019-05-06 06:11:35 -0500 )edit

please put your error msgs here, not on an external bin, where it will expire.

berak gravatar imageberak ( 2019-05-06 07:56:57 -0500 )edit

Here is as much of the error message as will fit in this comment section:

/usr/bin/ld: /home/moritz/opencv/opencv-4.1.0-built/lib64/libopencv_core.a(opencl_core.cpp.o): in function `opencl_check_fn(int)':

opencl_core.cpp:(.text._ZL15opencl_check_fni+0x1ba): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /usr/bin/ld: /home/moritz/opencv/opencv-4.1.0-built/lib64/libopencv_core.a(arithm.dispatch.cpp.o): in function cv::hal::add8u(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned char*, unsigned long, int, int, void*)': arithm.dispatch.cpp:(.text._ZN2cv3hal5add8uEPKhmS2_mPhmiiPv+0x89): undefined reference toippicviAdd_8u_C1RSfs' /usr/bin/ld: /home/moritz/opencv/o

Moritz10071996 gravatar imageMoritz10071996 ( 2019-05-06 08:01:58 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-05-06 08:12:11 -0500

berak gravatar image

for static linking, the order of libs matters, you have to sort them by dependancy.

if opencv_aruco depends on opencv_core, aruco has to go before core. you'll also need a ton of system libs, that were conveniently linked to you're so's before. here's a working (4.1.0) example:

 g++ -std=c++0x src/cv.cpp -I ocv4/include/opencv4 -L ocv4/lib -L ocv4/lib/opencv4/3rdparty -O3 -lopencv_aruco -lopencv_imgcodecs -lopencv_calib3d -lopencv_ccalib -lopencv_xfeatures2d -lopencv_features2d -lopencv_xobjdetect -lopencv_dnn -lopencv_flann -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_reg -lopencv_xphoto -lopencv_stitching -lopencv_superres -lopencv_bgsegm -lopencv_face -lopencv_saliency -lopencv_stitching -lopencv_superres -lopencv_tracking -lopencv_video -lopencv_ximgproc -lopencv_shape -lopencv_text -lopencv_optflow -lopencv_bioinspired -lopencv_imgproc -lopencv_core -littnotify -ljpeg -llibwebp -lrt -ldl -lz -lpthread -o src/cv
edit flag offensive delete link more


thank you for the response. After filling in the correct paths i tried to compile and got the errors: '/usr/bin/ld: cannot find -lopencv_aruco /usr/bin/ld: cannot find -lopencv_ccalib /usr/bin/ld: cannot find -lopencv_xfeatures2d /usr/bin/ld: cannot find -lopencv_xobjdetect /usr/bin/ld: cannot find -lopencv_reg /usr/bin/ld: cannot find -lopencv_xphoto /usr/bin/ld: cannot find -lopencv_superres /usr/bin/ld: cannot find -lopencv_bgsegm /usr/bin/ld: cannot find -lopencv_face /usr/bin/ld: cannot find -lopencv_saliency /usr/bin/ld: cannot find -lopencv_superres /usr/bin/ld: cannot find -lopencv_tracking ...' i removed those opencv libraries from the compile command since i dont need them anyway (or do i?). then i got a lot of undefined references again as errors.

Moritz10071996 gravatar imageMoritz10071996 ( 2019-05-06 09:06:33 -0500 )edit

i removed those

exactly ;) again, it was only an example, also assuming you've built with opencv_contrib (all those modules you don't have are from there.)

berak gravatar imageberak ( 2019-05-06 09:37:46 -0500 )edit

i am still missing some dependencies since i still get a lot of 'undefined reference to' errors. I am slowly managing to reduce their numbers by including the correct files. Currently i am struggling to find the library containing the 'Imf_2_3' namespace. any ideas?

Moritz10071996 gravatar imageMoritz10071996 ( 2019-05-06 09:47:52 -0500 )edit

'Imf_2_3 -- probably from exr/hdr support in imgcodecs.

note, that 3rdparty libs get installed to $your_prefix/lib/opencv4/3rdparty with current 4.x so please look, if you have related libs (???) there.

you can also disable it with cmake -DWITH_OPENEXR=OFF

berak gravatar imageberak ( 2019-05-06 10:12:03 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-05-05 09:25:44 -0500

Seen: 318 times

Last updated: May 06 '19