【问题标题】:Undefined symbols for architecture x86_64: std:terminate(), typeinfo, operator delete[], operator new[], and more [closed]架构 x86_64 的未定义符号:std:terminate()、typeinfo、operator delete[]、operator new[] 等 [关闭]
【发布时间】:2020-06-23 20:36:58
【问题描述】:

我正在学习 Lynda.com C++ 基础培训课程,但有些代码无法编译。下面的例子在编译时会报错:

// new-delete.cpp by Bill Weinman <http://bw.org/>
//   updated 2018-10-27
#include <cstdio>
#include <new>
using namespace std;

constexpr size_t count = 1024;

int main() {
    printf("allocate space for %lu long int at *ip with new\n", count);
    
    // allocate array
    long int * ip;
    
    try {
        ip = new long int [count];
    } catch (std::bad_alloc & ba) {
        fprintf(stderr, "Cannot allocate memory (%s)\n", ba.what());
        return 1;
    }
    
    // initialize array
    for( long int i = 0; i < count; ++i ) {
        ip[i] = i;
    }
    
    // print array
    for( long int i = 0; i < count; ++i ) {
        printf("%ld ", ip[i]);
    }
    puts("");
    
    // deallocate array
    delete [] ip;
    puts("space at *ip deleted");
    
    return 0;
}

这是错误:

Undefined symbols for architecture x86_64:
  "std::terminate()", referenced from:
      ___clang_call_terminate in new-delete-a02e2b.o
  "typeinfo for std::bad_alloc", referenced from:
      GCC_except_table0 in new-delete-a02e2b.o
  "operator delete[](void*)", referenced from:
      _main in new-delete-a02e2b.o
  "operator new[](unsigned long)", referenced from:
      _main in new-delete-a02e2b.o
  "___cxa_begin_catch", referenced from:
      _main in new-delete-a02e2b.o
      ___clang_call_terminate in new-delete-a02e2b.o
  "___cxa_end_catch", referenced from:
      _main in new-delete-a02e2b.o
  "___gxx_personality_v0", referenced from:
      _main in new-delete-a02e2b.o
      Dwarf Exception Unwind Info (__eh_frame) in new-delete-a02e2b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我正在用clang -std=c++11 编译代码。我正在通过终端编译代码,而不是通过 Xcode。我正在运行 macOS Mojave 10.14.6。

这很有可能是我犯了一个愚蠢的新手错误;如果是这样,请指出正确的方向,因为我已经研究了大约一个小时并且取得了零进展。

谢谢!

【问题讨论】:

  • clang 是一个 C 编译器。 clang++ 是 C++ 前端。
  • @eerorika 是的,就是这样......哎呀。非常感谢!

标签: c++ compilation linker clang std


【解决方案1】:

使用clang++,而不是clang

正如@eerorika 在 cmets 中指出的那样,clang 是 C 编译器,而不是 C++ 编译器。 clang++ 用于 C++。

【讨论】:

    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 2014-08-07
    • 2012-07-20
    • 2011-04-13
    • 2014-01-23
    • 1970-01-01
    相关资源
    最近更新 更多