具體報錯信息如下:
/usr/bin/ld: /usr/local/lib/libavcodec.a(fft.o): relocation R_X86_64_PC32 against symbol `ff_cos_32' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h263_loopfilter.o): relocation R_X86_64_PC32 against symbol `ff_h263_loop_filter_strength' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_chromamc.o): relocation R_X86_64_PC32 against symbol `ff_pw_8' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_qpel_10bit.o): relocation R_X86_64_PC32 against symbol `ff_pw_1023' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_qpel_8bit.o): relocation R_X86_64_PC32 against symbol `ff_pw_5' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(hevc_add_res.o): relocation R_X86_64_PC32 against symbol `ff_pw_1023' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(hevc_deblock.o): relocation R_X86_64_PC32 against symbol `ff_pw_m1' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(hevc_idct.o): relocation R_X86_64_PC32 against symbol `ff_pw_64' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(hevc_mc.o): relocation R_X86_64_PC32 against symbol `ff_pd_1' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(hevc_sao.o): relocation R_X86_64_PC32 against symbol `ff_pb_1' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(hevc_sao_10bit.o): relocation R_X86_64_PC32 against symbol `ff_pw_m1' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(hpeldsp.o): relocation R_X86_64_PC32 against symbol `ff_pb_1' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(idctdsp.o): relocation R_X86_64_PC32 against symbol `ff_pb_80' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(lossless_videodsp.o): relocation R_X86_64_PC32 against symbol `ff_pb_15' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(lossless_videoencdsp.o): relocation R_X86_64_PC32 against symbol `ff_pb_80' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(me_cmp.o): relocation R_X86_64_PC32 against symbol `ff_pb_1' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(pngdsp.o): relocation R_X86_64_PC32 against symbol `ff_pw_255' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(proresdsp.o): relocation R_X86_64_PC32 against symbol `ff_pw_1' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(qpeldsp.o): relocation R_X86_64_PC32 against symbol `ff_pw_3' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(rv40dsp.o): relocation R_X86_64_PC32 against symbol `ff_pw_32' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(utvideodsp.o): relocation R_X86_64_PC32 against symbol `ff_pb_80' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(v210enc.o): relocation R_X86_64_PC32 against symbol `ff_pw_4' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(vp3dsp.o): relocation R_X86_64_PC32 against symbol `ff_pb_1' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(vp6dsp.o): relocation R_X86_64_PC32 against symbol `ff_pw_64' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(vp8dsp.o): relocation R_X86_64_PC32 against symbol `ff_pw_256' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_cabac.o): relocation R_X86_64_PC32 against symbol `ff_h264_cabac_tables' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(ac3dsp.o): relocation R_X86_64_PC32 against symbol `ff_ac3_bap_bits' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(dirac_dwt.o): relocation R_X86_64_PC32 against symbol `ff_pw_2' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_chromamc_10bit.o): relocation R_X86_64_PC32 against symbol `ff_pw_8' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_deblock.o): relocation R_X86_64_PC32 against symbol `ff_pb_1' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_deblock_10bit.o): relocation R_X86_64_PC32 against symbol `ff_pw_4' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_idct.o): relocation R_X86_64_PC32 against symbol `ff_pw_32' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_idct_10bit.o): relocation R_X86_64_PC32 against symbol `ff_pd_32' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_intrapred.o): relocation R_X86_64_PC32 against symbol `ff_pb_3' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_intrapred_10bit.o): relocation R_X86_64_PC32 against symbol `ff_pw_1' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(h264_weight_10bit.o): relocation R_X86_64_PC32 against symbol `ff_pw_1' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libavcodec.a(sbrdsp.o): relocation R_X86_64_PC32 against symbol `ff_ps_neg' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libswscale.a(swscale.o): relocation R_X86_64_PC32 against symbol `ff_M24A' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/local/lib/libswscale.a(rgb2rgb.o): relocation R_X86_64_PC32 against symbol `ff_w1111' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: 最后的鏈結失敗: nonrepresentable section on output collect2: error: ld returned 1 exit status make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:250:lib/libopencv_videoio.so.4.5.2] 錯誤 1 make[1]: *** [CMakeFiles/Makefile2:2582:modules/videoio/CMakeFiles/opencv_videoio.dir/all] 錯誤 2 make: *** [Makefile:163:all] 錯誤 2
經排查發現是opencv在編譯的時候,用到了libswscale.a、libavcodec.a文件,這兩個文件應該是可選的,如果電腦中有這兩個文件,說明之前裝過ffmpeg或者用ffmpeg相關庫的軟件,這是在裝ffmpeg時安裝的,報上述錯誤可能是因為ffmpeg的libswscale.a、libavcodec.a文件版本和opencv有沖突,opencv在cmake的時候會檢測一些本機環境,如果有這兩個文件,就會讓這兩個文件參與到編譯當中。
解決辦法:
libswscale.a和libavcodec.a文件都在/usr/lib/x86_64-linux-gnu/目錄下
臨時重命名libswscale.a、libavcodec.a文件,例如重命名成libavcodec.a.bak、libswscale.a.bak,在opencv目錄下重新執行cmake,讓cmake檢測不到這兩個文件就可以了,
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/opt/dev/work/build_work/opencv/opencv-4.5.2/build_target(自定義的編譯路徑) -D BUILD_SHARED_LIBS=ON ..(opencv源碼路徑)
然后執行make -j或make -j[n],編譯opencv
經測試,opencv4.5.2,gcc 7.4.0, debian 10 編譯成功
