体系结构的未定义符号 x86_64:std:terminate()、typeinfo、operator delete[]、operator new[] 等
Undefined symbols for architecture x86_64: std:terminate(), typeinfo, operator delete[], operator new[], and more
我正在学习 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++
,而不是clang
正如@eerorika 在评论中指出的那样,clang
是 C 编译器,而不是 C++ 编译器。 clang++
适用于 C++。
我正在学习 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++
,而不是clang
正如@eerorika 在评论中指出的那样,clang
是 C 编译器,而不是 C++ 编译器。 clang++
适用于 C++。