Compilation 为什么在Alpine上使用musl编译失败而在ArchLinux上编译成功

Compilation 为什么在Alpine上使用musl编译失败而在ArchLinux上编译成功,compilation,musl,Compilation,Musl,这是一个好奇的问题:为什么? 为什么它对完全相同的代码有不同的行为? 我经常遇到这样的问题:我可以在一个发行版中编译一些东西,但不能在另一个发行版中编译。所以今天我又碰到了一个问题,当我以同样的方式在ArchLinux上构建PostgreSQL的pg_转储时,它可以工作,但当我在Alpine上进行时,它失败了,出现以下错误: gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-l

这是一个好奇的问题:为什么?

为什么它对完全相同的代码有不同的行为?

我经常遇到这样的问题:我可以在一个发行版中编译一些东西,但不能在另一个发行版中编译。所以今天我又碰到了一个问题,当我以同样的方式在ArchLinux上构建PostgreSQL的pg_转储时,它可以工作,但当我在Alpine上进行时,它失败了,出现以下错误:

gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -static -fPIC -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS -fPIC -shared -Wl,-soname,libpq.so.5 -Wl,--version-script=exports.list -o libpq.so.5.11 fe-auth.o fe-auth-scram.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o fe-protocol2.o fe-protocol3.o pqexpbuffer.o fe-secure.o libpq-events.o chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o pqsignal.o thread.o getpeereid.o pg_strong_random.o encnames.o wchar.o base64.o ip.o md5.o scram-common.o saslprep.o unicode_norm.o sha2.o -L../../../src/port -L../../../src/common    -Wl,--as-needed -Wl,-rpath,'/usr/local/pgsql/lib',--enable-new-dtags    
/lib/gcc/x86_64-linux-musl/8.2.0/../../../../x86_64-linux-musl/bin/ld: /lib/gcc/x86_64-linux-musl/8.2.0/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
/lib/gcc/x86_64-linux-musl/8.2.0/../../../../x86_64-linux-musl/bin/ld: /lib/gcc/x86_64-linux-musl/8.2.0/crtend.o: relocation R_X86_64_32 against `.ctors' can not be used when making a shared object; recompile with -fPIC
/lib/gcc/x86_64-linux-musl/8.2.0/../../../../x86_64-linux-musl/bin/ld: final link failed: nonrepresentable section on output
collect2: error: ld returned 1 exit status
make[1]: Leaving directory '/src/src/interfaces/libpq'
make[1]: *** [../../../src/Makefile.shlib:309: libpq.so.5.11] Error 1
make: *** [../../../src/Makefile.global:580: submake-libpq] Error 2
以下是阿尔卑斯山的Dockerfile:

FROM muslcc/x86_64:x86_64-linux-musl

RUN apk update && apk add make

ENV DOWNLOAD_URL https://ftp.postgresql.org/pub/source/v11.2/postgresql-11.2.tar.bz2

WORKDIR /src
RUN wget "$DOWNLOAD_URL" && \
    tar xvjf "${DOWNLOAD_URL##*/}" --strip-components=1 && \
    rm -fv "${DOWNLOAD_URL##*/}"

# NOTE: I left the -fPIC here for clarity sake but it fails with
#       the same error with or without it
RUN ./configure --without-readline --without-zlib CFLAGS="-static -fPIC"
RUN cd src/bin/pg_dump && make pg_dump
以下是ArchLinux的Dockerfile:

FROM archlinux/base

RUN pacman -Syu --noconfirm --needed base-devel musl

ENV DOWNLOAD_URL https://ftp.postgresql.org/pub/source/v11.2/postgresql-11.2.tar.bz2

WORKDIR /src
RUN curl -o "${DOWNLOAD_URL##*/}" "$DOWNLOAD_URL" && \
    tar xvjf "${DOWNLOAD_URL##*/}" --strip-components=1 && \
    rm -fv "${DOWNLOAD_URL##*/}"

RUN ./configure --without-readline --without-zlib CC="musl-gcc" CFLAGS="-static"
RUN cd src/bin/pg_dump && make pg_dump

我甚至不知道去哪里看。这可能是musl版本的不同吗?另一个编译工具?我真的不想要这个解决方案,我想了解原因。

我对Alpine Linux没有太多的经验,我需要自己使用它,但似乎一些非常基本的标准C库是作为非平台独立代码(PIC)构建的,这意味着不允许您将动态库与之链接

我将开始关注Alpine Linux开发人员,因为可能有PostgreSQL的配置标志,允许您构建所需的内容

现在,我注意到一件事。您将
CFLAGS=-static
传递给编译,但这毫无意义,因为
/configure
已经将其设置为共享-请仔细查看您引用的编译器调用。如果您想要静态构建,您需要找到一个合适的配置标志,并将其与
/configure
一起使用。我认为您的案例<代码>--禁用共享应该有效


似乎启用了
-static
,而没有离开
-shared

配置:警告:无法识别的选项:-禁用shared
否:(
CFLAGS="-static" CXXFLAGS="-static" LDFLAGS="-Wl,-Bstatic" ./configure --without-readline --without-zlib