Search code examples
c++dockercmakeglibcmusl

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


I'm attempting to compile a C++ library called VRPN with musl instead of glibc, and running into linker errors.

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

Save the above to a file and run docker build . to get the following error (after lots of successful building):

Output

[ 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

It seems that for some reason, musl's glibc hasn't been linked correctly, but I've been unable to figure out why.

I've read that linking to pre-compiled objects is generally the cause of this type of issue, but in this case, I'm pretty confident that everything is being built from source.

I don't have much experience with compiler internals, and I'm having a hard time finding reading material about musl simple enough that I can understand. Any insight or pointers would be much appreciated.

Thanks!


Solution

  • I managed to figure this one out. I think I was using host libraries rather than a proper musl toolchain. I got it to work by building in the messense/rust-musl-cross docker image (created with musl-cross-make), which contains the gcc toolchain compiled for the x86_64-unknown-linux-musl target.

    Here's my working docker build script for future reference.

    # 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
    

    Cheers!