在 libc++ 的 istringstream 的析构函数中未定义对运算符 delete 的引用

undefined reference to operator delete in destructor of istringstream from libc++

我尝试通过两种方式从主干 (~3.7) 构建 clang++:通过 gcc (4.8) 和 (old) clang++ (3.4和 3.5 来自数据包管理器)。它们都包含相同的步骤:

export CC=clang
export CXX=clang++
export CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:$HOME/llvm/projects/libcxxabi/include"

sudo apt-get install git

cd
git clone --single-branch --branch master --depth=1 http://llvm.org/git/llvm
cd llvm/projects/
git clone --single-branch --branch master --depth=1 http://llvm.org/git/libcxxabi
git clone --single-branch --branch master --depth=1 http://llvm.org/git/libcxx
git clone --single-branch --branch master --depth=1 http://llvm.org/git/compiler-rt
cd ../tools
git clone --single-branch --branch master --depth=1 http://llvm.org/git/clang
git clone --single-branch --branch master --depth=1 http://llvm.org/git/clang-tools-extra extra
git clone --single-branch --branch master --depth=1 http://llvm.org/git/lld
git clone --single-branch --branch master --depth=1 http://llvm.org/git/lldb
git clone --single-branch --branch master --depth=1 http://llvm.org/git/polly

sudo apt-get install python-dev libedit-dev libncurses-dev swig libgmp-dev libgmp3-dev dh-autoreconf libunwind8 libunwind8-dev cmake

cd
cd llvm/tools/polly/utils
mkdir -p ~/build-cloog
bash checkout_cloog.sh ~/build-cloog
mkdir -p ~/build-isl
bash checkout_isl.sh ~/build-isl
cd ~/build-isl/
./configure
make
sudo make install
cd ~/build-cloog/
./configure --with-isl=system
make
sudo make install

cd
mkdir build-llvm
cd build-llvm
bash ../llvm/configure --enable-optimized --disable-assertions --enable-libcpp --enable-jit --enable-targets=x86,x86_64 --enable-polly --enable-cxx1y --with-gmp=/usr/local --with-isl=/usr/local --with-cloog=/usr/local --with-binutils-include=/usr/include
make -j`nproc`
sudo make install

对于 CC=clangCXX=clang++ 我在链接包含 std::istringstream 实例化的简单示例时出错:

#include <sstream>

#include <cstdlib>

int
main()
{
        std::istringstream iss("1.1");
        double x;
        iss >> x;
        return EXIT_SUCCESS;
}

错误:

user@ubuntu:~$ clang++ -stdlib=libc++ -std=gnu++1z -Ofast -march=native test.cpp -o /tmp/test
/tmp/test-785a74.o: In function `std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_istringstream()':
test.cpp:(.text._ZNSt3__119basic_istringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEED0Ev[_ZNSt3__119basic_istringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEED0Ev]+0x5b): undefined reference to `operator delete(void*, unsigned long)'
/tmp/test-785a74.o: In function `virtual thunk to std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_istringstream()':
test.cpp:(.text._ZTv0_n24_NSt3__119basic_istringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEED0Ev[_ZTv0_n24_NSt3__119basic_istringstreamIcNS_11char_traitsIcEENS_9allocatorIcEEED0Ev]+0x69): undefined reference to `operator delete(void*, unsigned long)'
/tmp/test-785a74.o: In function `std::__1::basic_stringbuf<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_stringbuf()':
test.cpp:(.text._ZNSt3__115basic_stringbufIcNS_11char_traitsIcEENS_9allocatorIcEEED0Ev[_ZNSt3__115basic_stringbufIcNS_11char_traitsIcEENS_9allocatorIcEEED0Ev]+0x26): undefined reference to `operator delete(void*, unsigned long)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

但如果CC=gccCXX=g++,则不会出现错误。在这两种情况下,链接器都是 GNU Binutils 中的 ld

Exampli gratia 来自 #include <iostream> 的对象没有这样的问题。

错误的原因是什么?我可以在不重建整个 clang/llvm 项目树的情况下修复它吗?

clang 主干最近发生了变化。编译器不再发出大小运算符删除的弱定义(请参阅 commit 229241). The flag to emit the definitions anyway (-fdef-sized-delete) was renamed later in commit 229597.

您必须使用 -fdefine-sized-deallocation 标志编译您的程序,以便 clang 发出大小运算符删除。这应该可以解决您的 link 错误。