2

I have a project in C++ that uses OpenCV 3.1 and works fine using shared libaries. But now I want to compile it using static libraries (located in a folder within the project directory) because I want to be able to export it (and also edit and recompile if necessary) where OpenCV is not installed.

I have recompiled OpenCV this time setting shared libs to NO:

make -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_SHARED_LIBS=NO -DCMAKE_INSTALL_PREFIX=~/Desktop/ocv ..

Then I took my required libraries:

libopencv_core.a   libopencv_imgproc.a    libopencv_highgui.a
libopencv_video.a  libopencv_imgcodecs.a  libopencv_videoio.a

and ran g++ a.cpp libopencv_core.a where a.cpp is a sample program to test if everything works:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat a;
    printf("hello world\n" );
    return 0;
}

My problem is that I can not link the first library (core) because I get lots of undefined references like this:

libopencv_core.a(system.cpp.o): In function `cv::Mutex::Mutex()':
system.cpp:(.text._ZN2cv5MutexC2Ev+0x2c): undefined reference to `pthread_mutexattr_init'
system.cpp:(.text._ZN2cv5MutexC2Ev+0x39): undefined reference to `pthread_mutexattr_settype'
system.cpp:(.text._ZN2cv5MutexC2Ev+0x4c): undefined reference to `pthread_mutexattr_destroy'
libopencv_core.a(system.cpp.o): In function `cv::Mutex::trylock()':
system.cpp:(.text._ZN2cv5Mutex7trylockEv+0x8): undefined reference to `pthread_mutex_trylock'
libopencv_core.a(system.cpp.o): In function `cv::TlsAbstraction::TlsAbstraction()':
system.cpp:(.text._ZN2cv14TlsAbstractionC2Ev+0x9): undefined reference to `pthread_key_create'
libopencv_core.a(system.cpp.o): In function `cv::TlsAbstraction::~TlsAbstraction()':

and so on. I have searched all over and cannot find what's missing. Any help is greatly appreciated.

p.s. G++ and Ubuntu version: g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4

Leonardo Lanchas
  • 1,616
  • 1
  • 15
  • 37

1 Answers1

2

You need to link pthread library as well. And pass it as -pthread

g++ a.cpp libopencv_core.a -pthread

You're missing other libraries which contain the required code. There must be a libippicv.a which contains the code for ippicv* functions

g++ a.cpp libopencv_core.a libippicv.a -pthread

It should be somewhere among third_party libs.

nishantsingh
  • 4,537
  • 5
  • 25
  • 51
  • @ user3286661 I searched it and added it but as always not I have to look for the some gz lib --> libopencv_core.a(persistence.cpp.o): In function `icvPuts(CvFileStorage*, char const*)': persistence.cpp:(.text._ZL7icvPutsP13CvFileStoragePKc+0x22b): undefined reference to `gzputs' – Leonardo Lanchas Oct 07 '16 at 12:32
  • @Leo You now need libgzip perhaps. Also, the order in which the libraries are specified matters. So maybe you should look at that too. – nishantsingh Oct 07 '16 at 14:59
  • At everyone else reading this, @LeonardoLanchas is doing a static build. Therefore, he has not only to link against the static opencv libraries, but also the 3rd party libraries required by opencv. From the errors, guess which lib is needed next, add it, and you will see that the errors will decrease. – stackprotector Sep 28 '20 at 06:07