问题
After changing Ubuntu from 16.04 to 18.04 and OpenCV from 3.4.1 to 4.1.0 i can't compiled... anything
Step by step:
I downloaded source code from github, set those flags:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules cmake -DENABLE_PRECOMPILED_HEADERS=OFF -DBUILD_opencv_cudacodec=OFF -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_QT=OFF -D BUILD_SHARED_LIBS=OFF ..
compiled it and installed
Next, I created simple program:
#include "opencv2/highgui.hpp"
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
cout << "V: " << CV_VERSION << endl;
return 0;
}
Compiled it with this command:
g++ -std=c++11 main.cpp `pkg-config --libs --cflags opencv4` -g -o main
and get working executable main:
V: 4.1.0-pre
But when I added one line with Mat
#include "opencv2/highgui.hpp"
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
cout << "V: " << CV_VERSION << endl;
Mat frame;
return 0;
}
I got more errors than my terminal can display
I suppose, that files like libopencv_core.a are not linking
This method linking libraries always worked for me, I don't know where to look for a mistakes
Any clues, advice?
Edit:
Here is the begining of my error (full error contains +4500 lines, if it's necessary I can add .txt with full error)
/usr/local/lib/libopencv_core.a(system.cpp.o): In function `cv::ipp::getIPPSingleton()':
system.cpp:(.text._ZN2cv3ippL15getIPPSingletonEv+0xdb): undefined reference to `ippicvGetCpuFeatures'
system.cpp:(.text._ZN2cv3ippL15getIPPSingletonEv+0x16e): undefined reference to `ippicvSetCpuFeatures'
system.cpp:(.text._ZN2cv3ippL15getIPPSingletonEv+0x173): undefined reference to `ippicvGetEnabledCpuFeatures'
system.cpp:(.text._ZN2cv3ippL15getIPPSingletonEv+0x1ba): undefined reference to `ippicviGetLibVersion'
system.cpp:(.text._ZN2cv3ippL15getIPPSingletonEv+0x263): undefined reference to `pthread_mutexattr_init'
system.cpp:(.text._ZN2cv3ippL15getIPPSingletonEv+0x270): undefined reference to `pthread_mutexattr_settype'
system.cpp:(.text._ZN2cv3ippL15getIPPSingletonEv+0x283): undefined reference to `pthread_mutexattr_destroy'
system.cpp:(.text._ZN2cv3ippL15getIPPSingletonEv+0x2a1): undefined reference to `ippicvInit'
/usr/local/lib/libopencv_core.a(system.cpp.o): In function `cv::getInitializationMutex()':
system.cpp:(.text._ZN2cv22getInitializationMutexEv+0x63): undefined reference to `pthread_mutexattr_init'
system.cpp:(.text._ZN2cv22getInitializationMutexEv+0x70): undefined reference to `pthread_mutexattr_settype'
system.cpp:(.text._ZN2cv22getInitializationMutexEv+0x83): undefined reference to `pthread_mutexattr_destroy'
回答1:
I would run pkg-config --libs --cflags opencv4
in bash and check if the linker parameters are correct, and the paths/libraries exist. make install
should tell you where the libs are copied to (/usr/local/lib
or something like that). If #include
works, include directories are usually properly set.
回答2:
Got the same problem. It solves after making
sudo ldconfig
from the same folder I triggered the installation.
Give it a try, and good look.
回答3:
Finally I found a solution, this flag was guilty: -D BUILD_SHARED_LIBS=OFF
, after removing it everything worked fine
回答4:
While switching to dynamic linking may have solved this problem, you generally have to link against the missing libraries in a static build.
In this case you have to link at least against pthread
and libippicv
according to your error messages.
回答5:
static linking needs to include more dependencies
Your code
#include "opencv2/highgui.hpp"
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
cout << "V: " << CV_VERSION << endl;
Mat frame;
return 0;
}
builds on my system (win 8.1 64bit, opencv 4.2.0. compiled with BUILD_SHARED_LIBS=OFF, compiler mingw-w64) with the batch script
set gcc="C:\Program Files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin\g++.exe"
set path_opencv=C:\opencv-4.2.0-vc14_vc15\opencv\build
%gcc% code.cpp -I"%path_opencv%\include" -L"%path_opencv%\x64\mingw-w64\lib" -lopencv_core420 -lz -lpthread
As discussed on the opencv answers platform after turning BUILD_SHARED_LIBS to OFF , there are too many undefined references static linking does not forward dependencies. Therefore we need to additionally link the libraries -lz -lpthread
.
Further, the order of the -l... flags is important. This means open dependencies are collected and satisfied by the following libraries. Read Specify the libraries for the linker to use for more info.
correction to your code
It is sufficient to include #include <opencv2/core/core.hpp>
instead of highgui
and imgproc
. This is as cv::Mat is defined in core
. For this reason the flag -lopencv_core420
is enough and -lopencv_highgui -lopencv_imgproc
is not needed.
来源:https://stackoverflow.com/questions/55242450/linking-opencv-4-1-0-include-works-libs-doesnt