OpenCV linking error - c++

The same error first occured in the middle of the build when linking another shared library. Then I've added BUILD_SHARED_LIBS=OFF so now it occurs at the very end only. The last two options were also added in hope of fixing it. Using LinuxMint 17.
[ 99%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/deblurring.cpp.o
[ 99%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/motion_stabilizing.cpp.o
Linking CXX static library ../../lib/libopencv_videostab.a
[ 99%] Built target opencv_videostab
[ 99%] Generating pyopencv_generated_include.h, pyopencv_generated_funcs.h, pyopencv_generated_types.h, pyopencv_generated_type_reg.h, pyopencv_generated_ns_reg.h
Scanning dependencies of target opencv_python2
[ 99%] Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o
Linking CXX shared module ../../lib/cv2.so
/usr/bin/ld: //usr/local/lib/libavformat.a(allformats.o): relocation R_X86_64_32 against `ff_a64_muxer' can not be used when making a shared object; recompile with -fPIC
//usr/local/lib/libavformat.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make[2]: *** [lib/cv2.so] Error 1
make[1]: *** [modules/python2/CMakeFiles/opencv_python2.dir/all] Error 2
make: *** [all] Error 2
Command:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D PYTHON_EXECUTABLE=/home/serj/work/beautytorch/venv/bin/python \
-D BUILD_EXAMPLES=OFF \
-D BUILD_SHARED_LIBS=OFF \
-D CMAKE_CXX_FLAGS=-fPIC \
-D WITH_IPP=OFF \
..

It is ffmpeg which should have been compiled with -fPIC. enter link description here I had ffmpeg compiled from sources. After deinstallation, opencv links fine.

Related

macOS Monterey --> fatal error: 'opencv2/videoio.hpp' file not found

I am trying to build opencv on macOS for a CPP capstone project but when I try to compile like so:
cmake -D CMAKE_BUILD_TYPE=RELEASE -DBUILD_SHARED_LIBS=OFF -D BUILD_opencv_objdetect=OFF -D BUILD_opencv_video=OFF -D BUILD_opencv_videoio=OFF -D BUILD_opencv_features2d=OFF -D BUILD_opencv_flann=OFF -D BUILD_opencv_highgui=OFF -D BUILD_opencv_ml=OFF -D BUILD_opencv_photo=OFF -D BUILD_opencv_python=OFF -D BUILD_opencv_shape=OFF -D BUILD_opencv_stitching=OFF -D BUILD_opencv_superres=OFF -D BUILD_opencv_ts=OFF -D BUILD_opencv_videostab=OFF ..
make -j8
I am getting the following error:
[ 98%] Built target opencv_model_diagnostics
make[2]: *** No rule to make target `zlib', needed by `lib/python3/cv2.cpython-39-darwin.so'. Stop.
make[2]: *** Waiting for unfinished jobs....
[ 98%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o
In file included from /Users/username/Projects/opencv/opencv/modules/python/src2/cv2.cpp:11:
In file included from /Users/username/Projects/opencv/opencv/build/modules/python_bindings_generator/pyopencv_generated_include.h:50:
/Users/username/Projects/opencv/opencv/modules/gapi/include/opencv2/gapi/streaming/cap.hpp:26:10: fatal error: 'opencv2/videoio.hpp' file not found
#include "opencv2/videoio.hpp"
^~~~~~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
make[1]: *** [modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
make: *** [all] Error 2
I did follow at first the instruction from opencv but make step fails at:
[ 36%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/int8layers/layers_common.avx512_skx.cpp.o
[ 36%] Linking CXX shared library ../../lib/libopencv_dnn.dylib
[ 36%] Built target opencv_dnn
make: *** [all] Error 2
Any help is highly appreciated.

Compiling OpenCV errors

I trying to install opencv on my raspberry pi 3b+. Iam basing on tutorial from pyimagesearch.Iam on Step #5: Compile and Install OpenCV. I got errors after I execute make command. On 15% I got errors like on the screen.
Errors:
[ 22%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/layers/fully_connected_layer.cpp.o
/home/pi/opencv-3.1.0/modules/videoio/src/cap_ffmpeg_impl.hpp: In member function ‘bool OutputMediaStream_FFMPEG::open(const char*, int, int, double)’:
/home/pi/opencv-3.1.0/modules/videoio/src/cap_ffmpeg_impl.hpp:2207:30: warning: ignoring return value of ‘int avformat_write_header(AVFormatContext*, AVDictionary**)’, declared with attribute warn_unused_result [-Wunused-result]
avformat_write_header(oc_, NULL);
~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:154: modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:4209: modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 22%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/layers/im2col.cpp.o
[ 22%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/layers/layers_common.cpp.o
[ 22%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/layers/lrn_layer.cpp.o
[ 22%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/layers/mvn_layer.cpp.o
[ 22%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/layers/pooling_layer.cpp.o
[ 22%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/layers/reshape_layer.cpp.o
[ 22%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/layers/slice_layer.cpp.o
[ 22%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/layers/softmax_layer.cpp.o
[ 23%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/layers/split_layer.cpp.o
[ 23%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/torch/THDiskFile.cpp.o
[ 23%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/torch/THFile.cpp.o
[ 23%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/torch/THGeneral.cpp.o
[ 23%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/torch/torch_importer.cpp.o
[ 23%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/opencl_kernels_dnn.cpp.o
[ 23%] Linking CXX shared library ../../lib/libopencv_dnn.so
[ 23%] Built target opencv_dnn
make: *** [Makefile:163: all] Error 2
When my cmake looks like:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D WITH_TBB=OFF \
-D BUILD_EXAMPLES=ON ..
I got errors like:
make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:154: modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/Makefile2:4209: modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
make: *** [Makefile:163: all] Error 2
I will be greateful for any advice.
Okey, I handled the problem. My solution was to install brand new Raspberry Pi Buster image on sd card. Then I installed opencv basing on another tutorial from pyimagesearch.

dynamic library creation when installing with Cmake

I've been trying to install a package called DAKOTA, which employs BOOST and Trilinos libraries which I'm already using in another program. For compiling and linking I'm using cmake, but I have not been able to pass the make step. It would be very helpful perhaps if you could point me out into some direction. Please find below the cmake command script I'm using to compile and link the libraries, and the process when it stops...
As far as I have understood is the following. I'm using Boost and Trilinos library files used to build the other program to built the project DAKOTA. But I get an error when creating the library file liboptpp.so from Teuchos_BLAS.cpp.o, because it-s being used by libteuchosnumerics.a. I'm not sure if I'm understanding it correctly, but what I got from this is that I need to create a shared library and recompile the Teuchos_BLAS.cpp.o again using -fPIC.
I tried including -fPIC in CMAKE_CXX_FLAGS as you may see below, and also -shared in CMAKE_SHARED_LINKER_FLAGS but this doesn't seem to work. In the past I also tried adding -fPIC in all flags from the CMakeCache.txt but also did not work.
Please let me know your understanding and suggestions.
Thank you very much in advance,
CMAKE COMMAND
! /bin/bash
cmake \
-D CMAKE_INSTALL_PREFIX=/home/pablo/BuildDakota/build \
-D MPI_BASE_DIR:PATH="/usr/lib/openmpi/" \
-D CMAKE_CXX_FLAGS:STRING="-O0 -std=c++11 -pedantic -ftrapv -Wall -fPIC -Wno-long-long" \
-D BOOST_ROOT:PATH=/home/pablo/BuildPeridigm/ \
-D Boost_NO_BOOST_CMAKE:BOOL=TRUE \
-D Boost_NO_SYSTEM_PATHS:BOOL=TRUE \
-D Trilinos_DIR:PATH=/home/pablo/BuildPeridigm/lib/cmake/Trilinos/ \
-D CMAKE_SHARED_LINKER_FLAGS:STRING="-shared" \
/home/pablo/BuildDakota/dakota-6.7.0.src
PROCESS
[ 9%] Building C object packages/external/OPTPP/src/CMakeFiles/optpp.dir/PDS/sort.c.o
[ 9%] Building C object packages/external/OPTPP/src/CMakeFiles/optpp.dir/PDS/writes.c.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/BoolVector.C.o
[ 10%] Building C object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/file_cutils.c.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/ioformat.C.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/mcholesky.C.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/OptppExceptions.C.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/OptppFatalError.C.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/print.C.o
[ 10%] Building C object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/timers.c.o
[ 10%] Linking CXX shared library ../lib/liboptpp.so
/usr/bin/ld: /home/pablo/BuildPeridigm/lib/libteuchosnumerics.a(Teuchos_BLAS.cpp.o): relocation R_X86_64_32S against `_ZN7Teuchos11ETranspCharE' can not be used when making a shared object; recompile with -fPIC
/home/pablo/BuildPeridigm/lib/libteuchosnumerics.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
packages/external/OPTPP/src/CMakeFiles/optpp.dir/build.make:2264: recipe for target 'packages/external/OPTPP/lib/liboptpp.so' failed
make[2]: *** [packages/external/OPTPP/lib/liboptpp.so] Error 1
CMakeFiles/Makefile2:1409: recipe for target 'packages/external/OPTPP/src/CMakeFiles/optpp.dir/all' failed
make[1]: *** [packages/external/OPTPP/src/CMakeFiles/optpp.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
I tried including -fPIC in CMAKE_CXX_FLAGS as you may see below, and
also -shared in CMAKE_SHARED_LINKER_FLAGS but this doesn't seem to
work.
Don't do this. Adding low-level compiler flags manually is almost always a bad idea in CMake.
The general approach to request a project to build as a dynamic library is to set the BUILD_SHARED_LIBS option of the project:
cmake -DBUILD_SHARED_LIBS=ON [...]
If this doesn't work right away, it usually means that the project was not intended to be built as a dynamic library, in which case there is no easy solution.
Dakota, for instance, claims to support this option properly.
If this doesn't work, you can try playing around with options like CMAKE_POSITION_INDEPENDENT_CODE, but your chances of success will not be very high. You should probably contact the library maintainer for advice in this case.

No rule to make target 'TBB_ENV_LIB_DEBUG-NOTFOUND', needed by 'lib/libopencv_core.so.3.2.0

I'm trying to build opencv with the following CMAKE options:
cmake -D CMAKE_BUILD_TYPE=DEBUG -D CMAKE_INSTALL_PREFIX=/home/luca/Dropbox/SURFSPM/opencvInstall -D OPENCV_EXTRA_MODULES_PATH=/home/luca/ParallelOpenCV/opencv_contrib/modules -D WITH_TBB=ON -D WITH_OPENMP=ON -D ENABLE_PROFILING=ON ..
Notice that in RELEASE mode OpenCV is correctly compiled with all the other options. However, at 43% it returns this error:
[ 42%] Built target pch_Generate_opencv_perf_optflow
[ 42%] Built target pch_Generate_opencv_test_optflow
[ 43%] Generating precomp.hpp.gch/opencv_structured_light_DEBUG.gch
Scanning dependencies of target opencv_core
[ 43%] Built target pch_Generate_opencv_test_phase_unwrapping
make[2]: *** No rule to make target 'TBB_ENV_LIB_DEBUG-NOTFOUND', needed by 'lib/libopencv_core.so.3.2.0'. Stop.
make[2]: *** Waiting for unfinished jobs....
[ 43%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o
cc1plus: warning: /home/luca/ParallelOpenCV/opencv/build/modules/core/precomp.hpp.gch/opencv_core_Release.gch: created with -gnone, but used with -gdwarf-2
[ 43%] Built target pch_Generate_opencv_test_stitching
[ 43%] Built target pch_Generate_opencv_phase_unwrapping
[ 43%] Built target pch_Generate_opencv_test_structured_light
[ 43%] Built target pch_Generate_opencv_perf_stitching
[ 43%] Built target pch_Generate_opencv_stitching
[ 43%] Built target pch_Generate_opencv_structured_light
CMakeFiles/Makefile2:2161: recipe for target 'modules/core/CMakeFiles/opencv_core.dir/all' failed
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2
I think that this is Intel TBB related, but I've installed it (in fact it works with the RELEASE version and I don't know how to solve this.
Like #Tsyvarev's comment hints, indeed the configuration phase fails to pickup a debug library version.
I've gotten success setting the original setting:
TBB_ENV_LIB_DEBUG:FILEPATH=TBB_ENV_LIB_DEBUG-NOTFOUND
To the same value as TBB_ENV_LIB:FILEPATH
//Path to a library.
TBB_ENV_LIB:FILEPATH=/usr/lib/x86_64-linux-gnu/libtbb.so
//Path to a library.
TBB_ENV_LIB_DEBUG:FILEPATH=/usr/lib/x86_64-linux-gnu/libtbb.so

building opencv fail on Qiana

I encountered the problem during compiling OpenCV. Error message said that I don't have Eigen. However, I do have one in my /usr/local/include directory. Also, I ran CMake with option below.
CMake:
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D CUDA_GENERATION=Auto USE_EIGEN=/usr/local/include/Eigen ..
Compiling OpenCV(by make):
[ 75%] Building CXX object modules/contrib/CMakeFiles/opencv_contrib.dir/src/rgbdodometry.cpp.o
/home/gom7745/IR/opencv-2.4.11/modules/contrib/src/rgbdodometry.cpp:65:47: fatal error: unsupported/Eigen/MatrixFunctions: No such file or directory
# include <unsupported/Eigen/MatrixFunctions>
^
compilation terminated.
make[2]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/src/rgbdodometry.cpp.o] Error 1
make[1]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/all] Error 2
make: *** [all] Error 2
Does anyone have any idea about this? Thanks!!
Building it with -DWITH_EIGEN=OFF solved the issue for me.
here cmake is unable to find unsupported/Eigen/MatrixFunctions. First find where Eigen is installed, in my case it was located at /usr/local/include/eigen3. Now locate your rgbdodometry.cpp file, in my case it was in /opencv_contrib/modules/rgbd/src. Open the cpp file and modify the previous line with something like
"/usr/local/include/eigen3/unsupported/Eigen/MatrixFunctions"