Opencv4 static linking

asked 2019-08-07 10:29:34 -0500

h438 gravatar image

updated 2019-08-07 10:32:27 -0500

I built opencv statically cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/Libs/opencv/static .. \ -DBUILD_SHARED_LIBS=OFF \ -DBUILD_LIST="highgui,imgproc,imgcodecs"

After linking with a test example, I get the following errors

opencl_core.cpp:-1: error : undefined reference to `dlopen'
opencl_core.cpp:-1: error : undefined reference to `dlsym'
opencv/static/lib/libopencv_core.a(persistence.cpp.o):-1: In function `cv::FileStorage::Impl::release(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
file not found persistence.cpp:-1: error : undefined reference to `gzclose':

And many other similar undefined references, I do not use opencl at all, my example code is the following

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>

int main(int argc, char **argv) {
  if (argc != 2) {
    std::cout << "usage: DisplayImage.out <Image_Path>\n" << std::endl;
    return -1;
  }

  cv::Mat image = cv::imread(std::string{argv[1]});
  if (!image.data) {
      printf("No image data \n");
      return -1;
  }

  cv::rectangle(image, cv::Point(0, 0), cv::Point(112, 112),
                cv::Scalar(0, 255, 0), 10, cv::LINE_4);
  cv::putText(image, "Opencv", cv::Point(0, 112),
              cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, cv::Scalar(255, 0, 225));
  cv::imshow("Opencv 4", image);
  cv::waitKey(0);
  return 0;
}
edit retag flag offensive close merge delete

Comments

we'll need to see, how you try to link your final program.

with static linking, you have to sort the libs by dependancy, and you also have to link all system related libs, which before conveniently went into the opencv so's at library build time, like zlib, librt, lbmath, etc.

berak gravatar imageberak ( 2019-08-07 10:39:40 -0500 )edit

I am using Qt QMAKE, I think that it is straightforwrd

INCLUDEPATH += $$PWD/../../Libs/opencv/static/include/opencv4

INCLUDEPATH += $$PWD/../../Libs/opencv/static/lib/opencv4/3rdparty

LIBS += -L$HOME/Libs/opencv/static/lib -pthread -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs

LIBS += -L$$PWD/../../Libs/opencv/static/lib/opencv4/3rdparty/ -lade

PRE_TARGETDEPS += $$PWD/../../Libs/opencv/static/lib/libopencv_core.a

PRE_TARGETDEPS += $$PWD/../../Libs/opencv/static/lib/libopencv_highgui.a

PRE_TARGETDEPS += $$PWD/../../Libs/opencv/static/lib/libopencv_imgcodecs.a

PRE_TARGETDEPS += $$PWD/../../Libs/opencv/static/lib/libopencv_imgproc.a

PRE_TARGETDEPS += $$PWD/../../Libs/opencv/static/lib/opencv4/3rdparty/libade.a

h438 gravatar imageh438 ( 2019-08-07 12:04:08 -0500 )edit