微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 musl-g++

如何解决使用 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 的阅读材料,我可以理解。任何见解或指示将不胜感激。

谢谢!

解决方法

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

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

干杯!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。