使用 musl-g++ 从源代码构建时对“__printf_chk”的未定义引用

undefined reference to `__printf_chk' when building from source with musl-g++

我正在尝试使用 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 的阅读 material 足够简单以至于我能理解。任何见解或指示将不胜感激。

谢谢!

我设法解决了这个问题。我想我使用的是宿主库而不是合适的 musl 工具链。我通过构建 messense/rust-musl-cross docker image (created with musl-cross-make) 使其工作,其中包含为 x86_64-unknown-linux-musl 目标编译的 gcc 工具链。

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

# 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

干杯!