问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/39917056/undefined-references-in-static-opencv-libraries