背景:我在编译一个开源的链接器 mold, 想着用它加速一下本地的构建,但是构建的最后一步碰到了错误。
项目地址:
https://github.com/rui314/mold.git
编译步骤:
git clone https://github.com/rui314/mold.git
cd mold
git checkout v1.4.2
make -j`nproc` CXX=clang++ CXXFLAGS="-stdlib=libc++"
sudo make install
出错的文件:
clang++ -std=c++17 -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -DMOLD_VERSION=\"1.4.2\" -DLIBDIR="\"/usr/local
/lib\"" -Ithird-party/mimalloc/include -Ithird-party/tbb/include -MT out/macho/main.o -MMD -MP -MF out/macho/main.d -stdlib=libc++ -c -o out/macho/main.o macho/main.cc
报错信息:
macho/main.cc:453:11: error: no viable overloaded '+='
counter += std::erase_if(osec.members, [](Subsection<E> *subsec) {
~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
macho/main.cc:464:7: note: in instantiation of function template specialization 'mold::macho::uniquify_cstrings<mold::macho::X86_64>' requested here
uniquify_cstrings(ctx, *(OutputSection<E> *)chunk);
^
macho/main.cc:1052:3: note: in instantiation of function template specialization 'mold::macho::merge_cstring_sections<mold::macho::X86_64>' requested here
merge_cstring_sections(ctx);
^
macho/main.cc:998:14: note: in instantiation of function template specialization 'mold::macho::do_main<mold::macho::X86_64>' requested here
return do_main<X86_64>(argc, argv);
^
macho/main.cc:1121:10: note: in instantiation of function template specialization 'mold::macho::do_main<mold::macho::ARM64>' requested here
return do_main<ARM64>(argc, argv);
^
macho/../mold.h:643:12: note: candidate function not viable: cannot convert argument of incomplete type 'void' to 'int' for 1st argument
Counter &operator+=(int delta) {
^
macho/main.cc:453:11: error: no viable overloaded '+='
counter += std::erase_if(osec.members, [](Subsection<E> *subsec) {
~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
macho/main.cc:464:7: note: in instantiation of function template specialization 'mold::macho::uniquify_cstrings<mold::macho::ARM64>' requested here
uniquify_cstrings(ctx, *(OutputSection<E> *)chunk);
^
macho/main.cc:1052:3: note: in instantiation of function template specialization 'mold::macho::merge_cstring_sections<mold::macho::ARM64>' requested here
merge_cstring_sections(ctx);
^
macho/main.cc:1121:10: note: in instantiation of function template specialization 'mold::macho::do_main<mold::macho::ARM64>' requested here
return do_main<ARM64>(argc, argv);
^
macho/../mold.h:643:12: note: candidate function not viable: cannot convert argument of incomplete type 'void' to 'int' for 1st argument
Counter &operator+=(int delta) {
^
2 errors generated.
解决办法
这个问题比较明显是C++的版本库太低, 机器自带的版本是9.4.0:
$ /bin/gcc -v
Using built-in specs.
COLLECT_GCC=/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.4.0-1ubuntu1~20.04.1' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-Av3uEd/gcc-9-9.4.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
所以我需要一个更高版本的gcc, 用的这个镜像里的 10.4.0
http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/
编译按照官网来就行
./contrib/download_prerequisites
./configure --prefix=/mnt/e/workspace/gcc-10.4.0/install
make -j`nproc`
make install
export PATH=/mnt/e/workspace/gcc-10.4.0/install/bin:$PATH
有了这个库再回到我们的mold目录:
sudo time make -j6 CXX=clang++ CXXFLAGS=" -O3 --gcc-toolchain=/mnt/e/workspace/gcc-10.4.0/install "
make install
注:因为当前GCC 版本不支持 =std=c++20
所以这里用的还是clang, 强烈推荐使用 毕昇编译器