C++ 如何使用 -fPIC 重新编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13812185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:43:56  来源:igfitidea点击:

How to recompile with -fPIC

c++gcccompilationffmpegfpic

提问by user1455085

I was trying to reinstall my ffmpeg, following this guide, on my ARM Ubuntu machine. Unfortunately, when I compile a program which uses this lib I get the following failure:

我试图按照本指南在我的 ARM Ubuntu 机器上重新安装我的 ffmpeg 。不幸的是,当我编译一个使用这个库的程序时,我得到了以下失败:

/usr/bin/ld: /usr/local/lib/libavcodec.a(amrnbdec.o): relocation R_ARM_MOVW_ABS_NC against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libavcodec.a: could not read symbols: Bad value
collect2: ld returned 1 exit status

Now I would like to recompile it with -fPIClike the compiler is suggesting but I have no idea how. Any help is appreciated.

现在我想像-fPIC编译器建议的那样重新编译它,但我不知道如何。任何帮助表示赞赏。

回答by zaufi

in brief, the error means that you can't use a static library to be linked w/ a dynamic one. The correct way is to have a libavcodec compiled into .so instead of .a, so the other .so library you are trying to build will link well.

简而言之,该错误意味着您不能使用静态库与动态库链接。正确的方法是将 libavcodec 编译为 .so 而不是 .a,这样您尝试构建的另一个 .so 库就会链接良好。

the shortest way to do so is to add --enable-sharedat ./configureoptions. or even you may try to disable shared (or static) libraries at all... you choose what is suitable for you!

这样做的最短方法是添加--enable-sharedat./configure选项。甚至您可能会尝试完全禁用共享(或静态)库……您选择适合您的库!

回答by Kraiden

Have a look at this page.

看看这个页面。

you can try globally adding the flag using: export CXXFLAGS="$CXXFLAGS -fPIC"

您可以尝试使用以下方法全局添加标志: export CXXFLAGS="$CXXFLAGS -fPIC"

回答by dmaij

After the configure step you probably have a makefile. Inside this makefile look for CFLAGS (or similar). puf -fPIC at the end and run make again. In other words -fPIC is a compiler option that has to be passed to the compiler somewhere.

在配置步骤之后,您可能有一个 makefile。在这个 makefile 中寻找 CFLAGS(或类似的)。puf -fPIC 最后并再次运行 make。换句话说 -fPIC 是一个编译器选项,必须在某处传递给编译器。

回答by Tony Pavick

I hit this same issue trying to install Dashcast on Centos 7. The fix was adding -fPICat the end of each of the CFLAGS in the x264 Makefile. Then I had to run make distcleanfor both x264 and ffmpeg and rebuild.

我在尝试在 Centos 7 上安装 Dashcast 时遇到了同样的问题。修复是-fPIC在 x264 Makefile 中每个 CFLAGS 的末尾添加的。然后我不得不同时运行make distcleanx264 和 ffmpeg 并重建。

回答by Robert Lujo

I had this problem when building FFMPEG static libraries (e.g. libavcodec.a) for Android x86_64 target platform (using Android NDK clang). When statically linking with my library the problem occured although all FFMPEG C -> object files (*.o) were compiled with -fPIC compile option:

我在为 Android x86_64 目标平台(使用 Android NDK clang)构建 FFMPEG 静态库(例如 libavcodec.a)时遇到了这个问题。当与我的库静态链接时,尽管所有 FFMPEG C -> 目标文件 (*.o) 都是使用 -fPIC 编译选项编译的,但问题仍然存在:

x86_64/libavcodec.a(h264_qpel_10bit.o): 
requires dynamic R_X86_64_PC32 reloc against 'ff_pw_1023' 
which may overflow at runtime; recompile with -fPIC

The problem occured only for libavcodec.a and libswscale.a.

该问题仅出现在 libavcodec.a 和 libswscale.a 上。

Source of this problem is that FFMPEG has assembler optimizations for x86* platformse.g. the reported problem cause is in libavcodec/h264_qpel_10bit.asm-> h264_qpel_10bit.o.

此问题的根源在于FFMPEG 对 x86* 平台进行汇编优化,例如报告的问题原因在libavcodec/h264_qpel_10bit.asm-> h264_qpel_10bit.o。

When producing X86-64 bit static library (e.g. libavcodec.a) it looks like assembler files (e.g. libavcodec/h264_qpel_10bit.asm) uses some x86 (32bit) assembler commands which are incompatible when statically linking with x86-64 bit target librarysince they don't support required relocation type.

在生成 X86-64 位静态库(例如 libavcodec.a)时,它看起来像汇编程序文件(例如 libavcodec/h264_qpel_10bit.asm)使用一些 x86(32 位)汇编器命令,这些命令在与 x86-64 位目标库静态链接时不兼容,因为它们不支持所需的重定位类型。

Possible solutions:

可能的解决方案

  1. compile all ffmpeg files with no assembler optimizations (for ffmpeg this is configure option: --disable-asm)
  2. produce dynamic libraries (e.g. libavcodec.so) and link them in your final library dynamically
  1. 编译所有 ffmpeg 文件,没有汇编器优化(对于 ffmpeg,这是配置选项:--disable-asm)
  2. 生成动态库(例如 libavcodec.so)并将它们动态链接到最终库中

I chose 1) and it solved the problem.

我选择了 1),它解决了问题。

Reference: https://tecnocode.co.uk/2014/10/01/dynamic-relocs-runtime-overflows-and-fpic/

参考:https: //tecnocode.co.uk/2014/10/01/dynamic-relocs-runtime-overflows-and-fpic/

回答by Dark Coder

Before compiling make sure that "rules.mk" file is included properly in Makefile or include it explicitly by:

在编译之前,请确保“rules.mk”文件正确包含在 Makefile 中或通过以下方式明确包含:

"source rules.mk"

“源规则.mk”