Mac上编译C++报错

今天在使用Mac编译C++文件时,提示以下错误。

 Undefined symbols for architecture x86_64:
"std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
void std::__1::vector<E, std::__1::allocator<E> >::__push_back_slow_path<E const>(E const&) in main-8b5a99.o
"std::terminate()", referenced from:
___clang_call_terminate in main-8b5a99.o
"operator delete(void*)", referenced from:
std::__1::__vector_base<E, std::__1::allocator<E> >::~__vector_base() in main-8b5a99.o
std::__1::__split_buffer<E, std::__1::allocator<E>&>::~__split_buffer() in main-8b5a99.o
"operator new(unsigned long)", referenced from:
std::__1::__split_buffer<E, std::__1::allocator<E>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<E>&) in main-8b5a99.o
"___cxa_begin_catch", referenced from:
___clang_call_terminate in main-8b5a99.o
"___cxa_call_unexpected", referenced from:
_main in main-8b5a99.o
"___gxx_personality_v0", referenced from:
_main in main-8b5a99.o
___cxx_global_array_dtor in main-8b5a99.o
void std::__1::vector<E, std::__1::allocator<E> >::__push_back_slow_path<E const>(E const&) in main-8b5a99.o
Dwarf Exception Unwind Info (__eh_frame) in main-8b5a99.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code (use -v to see invocation)

通过在*翻阅找到相应的解决办法。 发生这种状况的原因是gcc默认文件时c文件,在编译时不会链接c++标准库。我们可以通过以下方式进行指定引入c++标准库

gcc main.c -o macin -lstdc++

或者使用

g++ main.c -o main

参考链接:*

上一篇:Code for the Homework1


下一篇:linux 内存介绍