iOS中鏈接混編C++庫的編譯問題

問題

從新封裝畫板內核庫後,全部的混編文件(.mm)都被打包到了庫中,外面應用層代碼所有都是OC文件(.m)。這時候編譯工程會出現以下鏈接錯誤:ui

Undefined symbols for architecture arm64: "vtable for __cxxabiv1::__vmi_class_type_info", referenced from: ... NOTE: a missing vtable usually means the first non-inline virtual member function has no definition. "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::assign(char const*)", referenced from: ... "___cxa_pure_virtual", referenced from: ... "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::operator=(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from: ... "std::__1::__vector_base_common<true>::__throw_out_of_range() const", referenced from: ... "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from: ... "std::terminate()", referenced from: ... "___cxa_guard_release", referenced from: ... "___cxa_guard_abort", referenced from: ... "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from: ... "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::reserve(unsigned long)", referenced from: ... "___gxx_personality_v0", referenced from: ... ... ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)code

探索

經過網上搜索到以下解決方案編譯器

  1. 將Build Settings->Apple LLVM7.1-Language中的Compile Sources As修改成According To File Tyle
  2. 將Build Settings->Apple LLVM7.1-Language中的Compile Sources As修改成Objective-C++

以上第二個方法對我不適用,修改成所有按**Objective-C++方式編譯源文件會出問題。第一個解決方案的話也有問題,因爲項目中沒有了混編文件(.mm),那麼Xcode根本不會調用Objective-C++**編譯器,因此鏈接問題仍是存在。string

解決

將項目中的某個OC文件(.m)強行改成後綴爲mm,使其成爲混編文件。 這種方法也適用於Swift工程,若是Swift也要引用C++的混編庫,那麼能夠增長一個空的混編文件(.mm),該文件不用實現,只是爲了欺騙IDE,調出相應編譯器完成鏈接。it

相關文章
相關標籤/搜索