Skip to content

zig c++ errors with boost #18770

@marchelbling-aqemia

Description

@marchelbling-aqemia

Zig Version

0.12.0-dev.2341+92211135f

Steps to Reproduce and Observed Behavior

I am trying to build some scientific tooling using zig so that I can have static binaries for a couple of architectures (and I'm pretty excited about zig for this!).

I am using the following Dockerfile [updated]:

# Usage: docker build -t smina:latest .

FROM debian:bullseye-slim as build
ARG BOOST_VERSION=1.75.0
ARG BESSENTIAL_VERSION=12.9
ARG OPENBABEL_VERSION=3.1.1
ARG EIGEN3_VERSION=3.3.9-2
ARG ZSTD_VERSION=v1.5.5
ARG ZLIB_VERSION=1.3.1
ARG SMINA_TAG=7369fc0
ARG ZIG_VERSION=0.12.0-dev.2341+92211135f
ARG ZIG_TARGET="x86_64-linux-gnu"
ENV DEBIAN_FRONTEND noninteractive


RUN mkdir /src \
 && apt-get update \
 && apt-get upgrade -y \
 && apt-get install -y -q \
        git \
        build-essential=${BESSENTIAL_VERSION} \
        cmake \
        cmake-curses-gui \
        wget \
        neovim \
        libeigen3-dev=${EIGEN3_VERSION}


WORKDIR /src
RUN git clone --recursive --depth 1 --branch "${ZSTD_VERSION}" https://github.com/facebook/zstd \
 && wget https://www.zlib.net/zlib-${ZLIB_VERSION}.tar.gz && tar -xf zlib-${ZLIB_VERSION}.tar.gz && mv zlib-${ZLIB_VERSION} zlib \
 && git clone --recursive --depth 1 --branch "boost-${BOOST_VERSION}" https://github.com/boostorg/boost \
 && git clone --depth 1 --branch "openbabel-$( echo ${OPENBABEL_VERSION} | tr '.' '-' )" https://github.com/openbabel/openbabel \
 && git clone --depth 1 git://git.code.sf.net/p/smina/code smina

WORKDIR /src
RUN wget https://ziglang.org/builds/zig-linux-$(uname -m)-${ZIG_VERSION}.tar.xz \
 && tar -xf zig-linux-$(uname -m)-${ZIG_VERSION}.tar.xz \
 && rm zig-linux-$(uname -m)-${ZIG_VERSION}.tar.xz \
 && mv zig-linux-$(uname -m)-${ZIG_VERSION} zig-${ZIG_VERSION}

ENV PATH "${PATH}:/src/zig-${ZIG_VERSION}"
ENV CC "zig cc -pthread -target ${ZIG_TARGET}"
ENV CXX "zig c++ -pthread -target ${ZIG_TARGET}"


WORKDIR /src/zstd
RUN mkdir tmp && cd tmp \
 && cmake \
        -DCMAKE_BUILD_TYPE=Release \
        -DBUILD_SHARED=OFF \
        -DCMAKE_INSTALL_PREFIX=/usr/local \
        -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
    ../build/cmake \
 && make -j8 install


WORKDIR /src/zlib
RUN mkdir build && cd build \
 && cmake \
        -DCMAKE_BUILD_TYPE=Release \
        -DBUILD_SHARED=OFF \
        -DCMAKE_INSTALL_PREFIX=/usr/local \
        -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
    .. \
 && make -j8 install


WORKDIR /src/boost
RUN ./bootstrap.sh --prefix=/usr/local --with-libraries=iostreams,timer,system,regex,thread,serialization,filesystem,program_options,date_time \
 && ./b2 variant=release link=static runtime-link=static install


WORKDIR /src/openbabel
RUN mkdir build && cd build \
 && cmake \
        -DBUILD_SHARED=OFF \
        -DCMAKE_INSTALL_PREFIX=/usr/local \
        -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
        # note: we fake PTHREAD_LIBRARY that will handled by the compiler correctly \
        -DPTHREAD_LIBRARY=/usr/local/lib \
    .. \
 && make -j8 install


WORKDIR /src/smina
RUN mkdir build && cd build \
 && sed -i "s/\bunordered_map</boost::unordered_map</" ../src/lib/CommandLine2/CommandLine.cpp \
 && cmake \
        -DCMAKE_BUILD_TYPE=Release \
        -DBUILD_SHARED_LIBS=OFF \
        -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
        -DBoost_INCLUDE_DIR=/usr/local/include \
        -DBoost_LIBRARY_DIR_RELEASE=/usr/local/lib \
        -DBOOST_TIMER_ENABLE_DEPRECATED=ON \
        -DBoost_USE_STATIC_LIBS=ON \
        -DOPENBABEL3_INCLUDE_DIR=/usr/local/include/openbabel3 \
        -DOPENBABEL3_LIBRARIES='/usr/local/lib/libopenbabel.a;/usr/local/lib/libz.a;/usr/local/lib/libzstd.a' \
    .. \
  && make smina -j8
  1. if I don't add the sed replacement before the last cmake, the compilation fails with zig (gcc (Debian 10.2.1-6) 10.2.1 20210110 compiles just fine)
/src/smina/src/lib/CommandLine2/CommandLine.cpp:1089:10: error: reference to 'unordered_map' is ambiguous
 1089 |     for (unordered_map<string, Option*>::iterator I = OptMap.begin(), E = OptMap.end();

Looking at the source (link), since <unordered_map> is not included, I would not expect the compiler to fail here, even if the code is

using namespace std;
using namespace boost;
  1. with the sed replacement, compilation works fine however the linker fails:
101.2 [100%] Linking CXX executable smina
102.2 ld.lld: error: undefined symbol: boost::program_options::option_description::long_name() const
102.2 >>> referenced by main.cpp:514 (/src/smina/src/main/main.cpp:514)
102.2 >>>               CMakeFiles/smina.dir/src/main/main.cpp.o:(get_occurrence(boost::program_options::variables_map&, boost::program_options::options_description&))
102.2 >>> referenced by main.cpp:531 (/src/smina/src/main/main.cpp:531)
102.2 >>>               CMakeFiles/smina.dir/src/main/main.cpp.o:(check_occurrence(boost::program_options::variables_map&, boost::program_options::options_description&))
102.2
...
102.2 ld.lld: error: undefined symbol: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::_M_create(unsigned long&, unsigned long)
102.2 >>> referenced by chrono.cpp
102.2 >>>               chrono.o:(boost::wrapexcept<boost::system::system_error>::clone() const) in archive /usr/local/lib/libboost_chrono.a
102.2 >>> referenced by chrono.cpp
102.2 >>>               chrono.o:(boost::wrapexcept<boost::system::system_error>::wrapexcept(boost::wrapexcept<boost::system::system_error> const&)) in archive /usr/local/lib/libboost_chrono.a
102.2 >>> referenced by convert.cpp
102.2 >>>               convert.o:(boost::program_options::to_internal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)) in archive /usr/local/lib/libboost_program_options.a
102.2 >>> referenced 202 more times
...

Expected Behavior

When relying on gcc (i.e. after removing the following lines:)

ENV PATH "${PATH}:/zig-${ZIG_VERSION}"
ENV CC "zig cc -pthread -target ${ZIG_TARGET}"
ENV CXX "zig c++ -pthread -target ${ZIG_TARGET}"

the build succeeds.

It seems that both issues somehow relate to the use of boost. I may be missing something obvious however I would expect that zig cc and zig c++ could be used as drop in replacements for gcc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions