2

我正在尝试用 musl 而不是 glibc 编译一个名为 VRPN 的 C++ 库,并遇到链接器错误。

Dockerfile

# This build container compiles fully static binaries for alpine
FROM ekidd/rust-musl-builder:1.49.0 AS build
ENV CC=musl-gcc
ENV CXX=musl-g++
ENV CFLAGS=-static
ENV CXXFLAGS=-static
USER root

# Install build dependencies
RUN apt-get update && apt-get install -y cmake

# Install VRPN (VR peripheral device network)
WORKDIR /usr/local/src
RUN git clone --depth 1 --branch v07.34 https://github.com/vrpn/vrpn.git
RUN cd vrpn && git submodule update --init --recursive
RUN mkdir vrpn_Build
WORKDIR /usr/local/src/vrpn_Build
RUN cmake -DCMAKE_BUILD_TYPE=RELEASE \
          -DVRPN_USE_GPM_MOUSE=OFF \
          -DVRPN_BUILD_CLIENT_LIBRARY=OFF \
          -DVRPN_BUILD_JAVA=OFF \
          -DVRPN_BUILD_PYTHON_HANDCODED_3X=OFF \
          -DVRPN_BUILD_SERVERS=OFF \
          -DVRPN_USE_LIBUSB_1_0=OFF \
          -DVRPN_USE_DEV_INPUT=OFF \
          -DVRPN_USE_HID=OFF \
          -DVRPN_USE_I2CDEV=OFF \
          -DVRPN_USE_JOYLIN=OFF \
          -DVRPN_USE_LIBUSB_1_0=OFF \
          ../vrpn

RUN make VERBOSE=1

将以上内容保存到文件并运行docker build .以获取以下错误(经过大量成功构建):

输出

[ 72%] Linking CXX executable time_test
/usr/bin/cmake -E cmake_link_script CMakeFiles/time_test.dir/link.txt --verbose=1
/usr/bin/musl-g++  -static -fPIC -O3 -DNDEBUG  -rdynamic CMakeFiles/time_test.dir/time_test.cpp.o  -o time_test  -L/usr/lib/x86_64-linux-musl libvrpnserver.a quat/libquat.a -lm atmellib/libvrpn_atmel.a gpsnmealib/libgpsnmea.a -Wl,-Bstatic -lgcc -lgcc_eh 
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x12): undefined reference to `__libc_csu_fini'
(.text+0x19): undefined reference to `__libc_csu_init'
CMakeFiles/time_test.dir/time_test.cpp.o: In function `main':
time_test.cpp:(.text.startup+0x45): undefined reference to `__printf_chk'
time_test.cpp:(.text.startup+0xee): undefined reference to `__printf_chk'
time_test.cpp:(.text.startup+0x120): undefined reference to `__printf_chk'
collect2: error: ld returned 1 exit status
CMakeFiles/time_test.dir/build.make:100: recipe for target 'time_test' failed
make[2]: Leaving directory '/usr/local/src/vrpn_Build'
make[2]: *** [time_test] Error 1
CMakeFiles/Makefile2:973: recipe for target 'CMakeFiles/time_test.dir/all' failed
make[1]: Leaving directory '/usr/local/src/vrpn_Build'
make[1]: *** [CMakeFiles/time_test.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
The command '/bin/sh -c make VERBOSE=1' returned a non-zero code: 2

似乎由于某种原因,musl 的glibc链接不正确,但我一直无法弄清楚原因。

我读过链接到预编译对象通常是导致此类问题的原因,但在这种情况下,我非常有信心一切都是从源代码构建的。

我对编译器内部没有太多经验,而且我很难找到关于 musl 的阅读材料足够简单以至于我可以理解。任何见解或指示将不胜感激。

谢谢!

4

1 回答 1

2

我设法弄清楚了这一点。我想我使用的是主机库而不是适当的 musl 工具链。我通过构建messense/rust-musl-cross docker映像(使用musl-cross-make 创建)来使其工作,其中包含为目标gcc编译的工具链。x86_64-unknown-linux-musl

这是我的工作 docker build 脚本以供将来参考。

# This build container compiles fully static binaries for alpine
FROM messense/rust-musl-cross:x86_64-musl AS build
USER root

# Install build dependencies
RUN apt-get update && apt-get install -y cmake

# Install VRPN (VR peripheral device network)
WORKDIR /usr/local/src
RUN git clone --depth 1 --branch v07.34 https://github.com/vrpn/vrpn.git
RUN cd vrpn && git submodule update --init --recursive
RUN mkdir vrpn_Build
WORKDIR /usr/local/src/vrpn_Build
RUN set -x

ENV TARGET=x86_64-unknown-linux-musl
ENV CC=$TARGET-gcc
ENV CXX=$TARGET-g++

RUN cmake -DCMAKE_BUILD_TYPE=Release \
          -DVRPN_USE_GPM_MOUSE=OFF \
          -DVRPN_BUILD_JAVA=OFF \
          -DVRPN_BUILD_PYTHON_HANDCODED_3X=OFF \
          -DVRPN_BUILD_SERVERS=OFF \
          -DVRPN_BUILD_CLIENTS=OFF \
          -DVRPN_USE_LIBUSB_1_0=OFF \
          -DVRPN_USE_DEV_INPUT=OFF \
          -DVRPN_USE_HID=OFF \
          -DVRPN_USE_I2CDEV=OFF \
          -DVRPN_USE_JOYLIN=OFF \
          -DVRPN_USE_LIBUSB_1_0=OFF \
          ../vrpn

RUN make VERBOSE=1

干杯!

于 2021-01-25T01:54:43.947 回答