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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM