Estoy tratando de aprender C ++ en macOS 11.2 (x86-64) y tengo problemas para compilar y vincular mi código. Mi programa simple se enumera a continuación.

#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;
}

Estoy intentando compilar con clang -std=c++11 <filename>, pero recibo el siguiente mensaje de error:

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)

Intenté agregar la bandera -stdlib=libstdc++, pero parece que libstdc++ ya no se incluye con macOS. ¿Hay algo más que deba hacer?

0
Sam Henke 12 mar. 2021 a las 21:16

1 respuesta

La mejor respuesta

Según Konrad Rudolph, la solución es usar clang++ en lugar de clang para compilar código C ++.

1
Sam Henke 12 mar. 2021 a las 21:47