spo*_*rkl 1 c++ linker compilation std clang
我正在学习 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;
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
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)
Run Code Online (Sandbox Code Playgroud)
我正在用 编译代码clang -std=c++11。我通过终端编译代码,而不是通过 Xcode。我运行的是 macOS Mojave 10.14.6。
我很有可能犯了一个愚蠢的新手错误;如果是这样,请为我指出正确的方向,因为我已经研究了大约一个小时并且取得了零进展。
谢谢你!
| 归档时间: |
|
| 查看次数: |
1519 次 |
| 最近记录: |