我如何在 macOS 上 link libc++?

How can I link libc++ on macOS?

我正在尝试在 macOS 11.2 (x86-64) 上学习 C++,但在编译和链接我的代码时遇到了问题。下面列出了我的简单程序。

#include <algorithm>
#include <vector>

template<class I>
void pancake_sort(I first, I last)
{
    for (; first != last; first++){
        std::reverse(std::min_element(first, last), last);
        std::reverse(first, last);
    }
}

int main(int argc, char *argv[])
{
    auto a = std::vector<int64_t>(argc - 1);
    std::transform(&argv[1], &argv[1] + (argc - 1), a.begin(), atoll);
    pancake_sort(a.begin(), a.end());

    return 0;
}

我正在尝试使用 clang -std=c++11 <filename> 进行编译,但收到以下错误消息:

Undefined symbols for architecture x86_64:
  "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
      std::__1::vector<long long, std::__1::allocator<long long> >::__vallocate(unsigned long) in pancake_sort-cdbf1e.o
  "std::logic_error::logic_error(char const*)", referenced from:
      std::length_error::length_error(char const*) in pancake_sort-cdbf1e.o
  "std::length_error::~length_error()", referenced from:
      std::__1::__throw_length_error(char const*) in pancake_sort-cdbf1e.o
  "std::terminate()", referenced from:
      ___clang_call_terminate in pancake_sort-cdbf1e.o
  "typeinfo for std::length_error", referenced from:
      std::__1::__throw_length_error(char const*) in pancake_sort-cdbf1e.o
  "vtable for std::length_error", referenced from:
      std::length_error::length_error(char const*) in pancake_sort-cdbf1e.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "operator delete(void*)", referenced from:
      std::__1::_DeallocateCaller::__do_call(void*) in pancake_sort-cdbf1e.o
  "operator new(unsigned long)", referenced from:
      std::__1::__libcpp_allocate(unsigned long, unsigned long) in pancake_sort-cdbf1e.o
  "___cxa_allocate_exception", referenced from:
      std::__1::__throw_length_error(char const*) in pancake_sort-cdbf1e.o
  "___cxa_begin_catch", referenced from:
      ___clang_call_terminate in pancake_sort-cdbf1e.o
  "___cxa_free_exception", referenced from:
      std::__1::__throw_length_error(char const*) in pancake_sort-cdbf1e.o
  "___cxa_throw", referenced from:
      std::__1::__throw_length_error(char const*) in pancake_sort-cdbf1e.o
  "___gxx_personality_v0", referenced from:
      _main in pancake_sort-cdbf1e.o
      std::__1::vector<long long, std::__1::allocator<long long> >::vector(unsigned long) in pancake_sort-cdbf1e.o
      std::__1::__vector_base<long long, std::__1::allocator<long long> >::__vector_base() in pancake_sort-cdbf1e.o
      std::__1::vector<long long, std::__1::allocator<long long> >::__construct_at_end(unsigned long) in pancake_sort-cdbf1e.o
      std::__1::vector<long long, std::__1::allocator<long long> >::max_size() const in pancake_sort-cdbf1e.o
      std::__1::__throw_length_error(char const*) in pancake_sort-cdbf1e.o
      std::__1::__vector_base<long long, std::__1::allocator<long long> >::__destruct_at_end(long long*) in pancake_sort-cdbf1e.o
      ...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我尝试添加标志 -stdlib=libstdc++,但 macOS 似乎不再包含 libstdc++。还有什么我应该做的吗?

根据 Konrad Rudolph 的说法,解决方案是使用 clang++ 而不是 clang 来编译 C++ 代码。