【问题标题】:undefined reference to `__printf_chk' when building from source with musl-g++使用 musl-g++ 从源代码构建时对“__printf_chk”的未定义引用
【发布时间】:2021-04-28 17:12:27
【问题描述】:

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

谢谢!

【问题讨论】:

  • __printf_chk 听起来有些工件是在调试模式下编译的,并且在发布版本等时缺少其中的引用。我会朝那个方向进行调查。

标签: c++ docker cmake glibc musl


【解决方案1】:

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

这是我的工作 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

干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-23
    • 2018-06-27
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2014-01-09
    相关资源
    最近更新 更多