记录linux编译opencv时报错: relocation R_X86_64_PC32 against symbol `XXXX' can not be used when making a shared object; recompile with -fPIC
具体报错信息如下:
/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 编译成功