Mac上编译C++报错

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

 1 Undefined symbols for architecture x86_64:
 2   "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
 3       void std::__1::vector<E, std::__1::allocator<E> >::__push_back_slow_path<E const>(E const&) in main-8b5a99.o
 4   "std::terminate()", referenced from:
 5       ___clang_call_terminate in main-8b5a99.o
 6   "operator delete(void*)", referenced from:
 7       std::__1::__vector_base<E, std::__1::allocator<E> >::~__vector_base() in main-8b5a99.o
 8       std::__1::__split_buffer<E, std::__1::allocator<E>&>::~__split_buffer() in main-8b5a99.o
 9   "operator new(unsigned long)", referenced from:
10       std::__1::__split_buffer<E, std::__1::allocator<E>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<E>&) in main-8b5a99.o
11   "___cxa_begin_catch", referenced from:
12       ___clang_call_terminate in main-8b5a99.o
13   "___cxa_call_unexpected", referenced from:
14       _main in main-8b5a99.o
15   "___gxx_personality_v0", referenced from:
16       _main in main-8b5a99.o
17       ___cxx_global_array_dtor in main-8b5a99.o
18       void std::__1::vector<E, std::__1::allocator<E> >::__push_back_slow_path<E const>(E const&) in main-8b5a99.o
19       Dwarf Exception Unwind Info (__eh_frame) in main-8b5a99.o
20 ld: symbol(s) not found for architecture x86_64
21 clang: error: linker command failed with exit code 1 (use -v to see invocation)

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

gcc main.c -o macin -lstdc++

或者使用

g++ main.c -o main

参考链接:stackoverflow

原文地址:https://www.cnblogs.com/WingPig/p/5256920.html