Opencv Static Linking Gives Error - c++

Recently I build opencv as static library(by using BUILD_SHARED_LIBS=OFF) while dynamic libraries are already there, but at different location. And when I build may program I am getting many error. I am using command line for compiling the code, and look like below.
g++ -static -I$(OpenCV_Static_Build)/include/opencv -L$(OpenCV_Static_Build)/lib/ -g -o binary main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy
The error look like,
grfmt_tiff.cpp:(.text._ZN2cv11TiffEncoder12writeLibTiffERKNS_3MatERKSt6vectorIiSaIiEE+0x167): undefined reference to `TIFFSetField'
window_gtk.cpp:(.text.cvNamedWindow+0x29d): undefined reference to `g_type_check_instance_cast'
/usr/lib/gcc/i486-linux-gnu/4.4.3/libgcc_eh.a(unwind-dw2.o): In function `uw_init_context_1':
(.text+0x22f8): undefined reference to `pthread_once'

Those errors don't come from OpenCV (as one can tell from the function names) but from GLib and libpthread. Use the -lglib-2.0 -lpthread linker flags.

Related

apple mach-O linker (id) error with open cv 3.2.0

I installed OpenCV 3.2.0-dev on my mac(10.12) using Homebrew following this instruction http://www.pyimagesearch.com/2015/06/15/install-opencv-3-0-and-python-2-7-on-osx/.
I solved the QTKit.h problem by skipping the checkout step.
Then I tried to build a small c++ program in Xcode 8 because I'm more familiar with c++ than python, it gave me an Apple mach-O linker (id) error which is shown here:
error message screenshot
The search paths for header and library are /usr/local/include and /usr/local/lib respectively. And the linker flags are
-lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videoio -lopencv_videostab
And I know when I use OpenCV version later than 3.1(I think), the linker flags can be replaced by something like opencv_world310.lib. Is there a similar way in Xcode?
Anyone can help me with that?
Homebrew install opencv in /usr/local/Cellar/, not in /usr/local/. So you need to modify header and library search paths correspondingly.

OpenCV "Undefined reference to 'cv::imread' etc C++

So I've written some very basic OpenCV C++ code to test it's functionality. I am using CodeBlocks on Linux Ubuntu 16.04. I have installed OpenCV correctly, I think, Codeblocks offers 'OpenCV Project' as an option when making a project and whenever I start typing in an OpenCV keyword, it'll expand and suggest keywords.
My problem is that when I attempt to build it, I get
undefined reference to `cv::imread(std::__cxxll::basic_string<char, std::char_traits<ch..
And it repeats this for every opencv function.
I read somewhere that this may have something to do with the archive files and so I'll provide the results when I enter:
pkg-config opencv --libs
into the terminal. The output is:
-L/usr/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core
-lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui
-lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree
-lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching
-lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -latomic
-ltbb -lGL -lGLU -lrt -lpthread -lm -ldl
So how can I solve this issue? Should I edit my files in some way?
Thanks very much in advance
Your code has been compiled with a standard library using the std::__cxx11 namespace. Ie, you're probably using g++ 5+ which has a different ABI to previous versions. The opencv libraries you've installed are probably an older ABI, and therefore not compatible.
You can either compile the opencv libraries yourself, use an earlier compiler that matches the ABI of the libries you have, or configure your compiler to use the old ABI.
You can use the macro _GLIBCXX_USE_CXX11_ABI if you're using g++ 5. Before you include and std libs:
#define _GLIBCXX_USE_CXX11_ABI 0

opencv: undefined reference to `cv::cuda::DescriptorMatcher::createBFMatcher(int)'

I'm trying to complie opencv-3.1.0/samples/gpu/surf_keypoint_matcher.cpp through:
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binary surf_keypoint_matcher.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_stitching -lopencv_imgcodecs -lopencv_xfeatures2d
But I'm getting this error:
/tmp/ccQW7t3Z.o: In function `main':
/home/luca/Downloads/opencv-3.1.0/samples/gpu/surf_keypoint_matcher.cpp:65: undefined reference to `cv::cuda::DescriptorMatcher::createBFMatcher(int)'
collect2: error: ld returned 1 exit status
Why?
Your error message:
/home/luca/Downloads/opencv-3.1.0/samples/gpu/surf_keypoint_matcher.cpp:65: undefined reference to `cv::cuda::DescriptorMatcher::createBFMatcher(int)'
collect2: error: ld returned 1 exit status
shows that you have an undefined reference to cv::cuda::DescriptorMatcher::createBFMatcher
this resides in the cuda libs so you need to include the appropriate headers and link into the following libs as opencv_cudafeatures2d depends on them:
opencv_cudafilters
opencv_cudawarping
opencv_features2d
opencv_cudaarithmn
opencv_flann
opencv_core
certainly this is true for opencv version 3.1.0 which is what I'm using
DescriptorMatcher is in cudafeatures2d.hpp. So, you have to include the header cudafeatures2d.hpp in the .cpp program and link the library in the compile process.
The program runs if you use this -lopencv_cudafeatures2d:
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binary surf_gpu_probe1.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_stitching -lopencv_imgcodecs -lopencv_xfeatures2d -lopencv_cudafeatures2d

undefined reference to some of the opencv functions

I'm trying to compile the 'motemple.c' that comes with the opencv samples in Ubuntu 14.04 environment. I think that opencv has been properly installed; most of the programs are running as expected. However, when compiling the above mentioned file, an error "undefined reference" is raised for several functions. Below is the output of the compilation:
Building target: ACTION_detection1
Invoking: Cross G++ Linker
g++ -L/usr/local/lib -o "ACTION_detection1" ./opencvexample/MotionSegmentation.o -lopencv_core -lopencv_photo -lopencv_videoio -lopencv_videostab -lopencv_imgcodecs -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
./opencvexample/MotionSegmentation.o: In function `update_mhi':
/media/thanuja/DTR_DATA/ACTION-TV/ACTIONDETECTION/Implementations/ACTION_detection1/Debug/../opencvexample/MotionSegmentation.cpp:103: undefined reference to `cvUpdateMotionHistory'
/media/thanuja/DTR_DATA/ACTION-TV/ACTIONDETECTION/Implementations/ACTION_detection1/Debug/../opencvexample/MotionSegmentation.cpp:105: undefined reference to `cvUpdateMotionHistory'
/media/thanuja/DTR_DATA/ACTION-TV/ACTIONDETECTION/Implementations/ACTION_detection1/Debug/../opencvexample/MotionSegmentation.cpp:114: undefined reference to `cvCalcMotionGradient'
/media/thanuja/DTR_DATA/ACTION-TV/ACTIONDETECTION/Implementations/ACTION_detection1/Debug/../opencvexample/MotionSegmentation.cpp:123: undefined reference to `cvSegmentMotion'
/media/thanuja/DTR_DATA/ACTION-TV/ACTIONDETECTION/Implementations/ACTION_detection1/Debug/../opencvexample/MotionSegmentation.cpp:149: undefined reference to `cvCalcGlobalOrientation'
collect2: error: ld returned 1 exit status
make: *** [ACTION_detection1] Error 1
As it can be seen, I have linked all the opencv libraries to mu Eclipse project. I also tried the command line solution provided here, but still get the same error.
they have been replaced to the opencv_contrib from the main repository. You can find them in the optflow module.
Notice: cvUpdateMotionHistory, cvCalcMotionGradient, etc. is the name of OpenCV 1.x API functions, they are now obsoleted/outdated so try to avoid them by replacing to cv::updateMotionHistory, cv::calcMotionGradient, etc.

Eclipse CDT not working with OpenCV 2.0 integrated with Cygwin compiler

I have done all the necessary job like adding "c:/cygwin/bin" to system path, I have also set the include path under project->properties->c/c++ build->setting and also set the linker library search path.
Now when I include file like #include"cv.h", it gives no error, but when I run the program error comes like
undefined reference to '_cvGetSize'
I have included the necessary files, and read lots of tutorials that are available but still I couldn't able to run my code, please help me
I always recommend that before jumping to an IDE you make sure your system is installed and configured correctly by attempting to compile an application on the cmd-line:
g++ flann.cpp -o flann `pkg-config --cflags --libs opencv`
or:
g++ flann.cpp -o flann -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
Apparently you didn't link your project with the opencv libraries, which is being achieved in the cmd above with: -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann.
That missing symbol is defined in one of these libraries.