Compile errors with C++ static library include in Swift project

I created a static library that includes the follow C++ files:

//TestClass.h File: #ifndef TESTCLASS_H_ #define TESTCLASS_H_ using namespace std; #include <string> class TestClass { public: TestClass(); virtual ~TestClass(); int sum(int x, int y) const; string chain(const string& x, const string& y) const; }; #endif /* TESTCLASS_H_ */ //TestClass.cpp File: #include<iostream> #include "TestClass.h" TestClass::TestClass() { } TestClass::~TestClass() { } int TestClass::sum(int x, int y) const { return x+y; } //Test.cpp File: string TestClass::chain(const string& x, const string& y) const { return x+y; } int main(int argc, char* argv[]) { TestClass test; cout << "1+1 = " << test.sum(1,1) << endl; cout << "Dog+Cat = " << test.chain("Dog","Cat") << endl; return 0; }

I added

-x objective-c++

flag in "Compile Source" and


flag in "Info.plist Other Preprocessor flags".

When I link my just created static library (with Objective C wrapper files), I receive the 4 follow errors, that I don't have any idea how to fix it:

Undefined symbols for architecture arm64: "vtable for __cxxabiv1::__class_type_info", referenced from: typeinfo for TestClass in libPredictionComplete.a(TestClass.o) NOTE: a missing vtable usually means the first non-inline virtual member function has no definition. "operator delete(void*)", referenced from: TestClass::~TestClass() in libPredictionComplete.a(TestClass.o) "___gxx_personality_v0", referenced from: -[CppObject init] in libPredictionComplete.a(CppObject.o) -[PredictionComplete init] in libPredictionComplete.a(PredictionComplete.o) -[PredictionComplete chain::] in libPredictionComplete.a(PredictionComplete.o) ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'll appreciate any ideas about.


From my experience, Xcode will fail to link against C++ libraries when there are no C++ sources present in the top-level project. This is also true in pure Obj-C projects as well where you are linking against static libraries that have C++ code.

A less invasive solution is to simply include an empty C++ source file in your project (here's an example cpp stub) that is "compiled" into the final .app binary. It actually doesn't emit any code, but it makes Xcode aware that there is C++ code present, causing C++ libraries to be linked in.

The advantage of this solution is it avoids modifying the project settings in Xcode, so no need to add special linker flags to force Xcode to do the right thing.


<img src="https://i.stack.imgur.com/lGbcF.png" alt="These are the C++ settings for linking with the standard library">

You should have -x c++ rather than objective-c++. There is, however, no need to specify this flag if you name your C++ sorce *.cpp.


I fixed it when I added the "-lstdc++" compile flag to "other linker flags" section in the Swift Project itself and not only in the static library project with c++ files.


The -lstdc++ should be in "Other Linker Flags" (or OTHER_LDFLAGS). Note that that will only work if the "C++ Standard Library" is set to libstdc++.

Also of note is that Xcode is usually smart enough to include the standard C++ library if the target has any C++ source code in it, so you don't need to explicitly link to either libstdc++ or libc++.


  • Stringify object name in Lua
  • Vim syntax highlighting for multiline fortran openmp directives
  • How to inject the right version information into the resources at compile time?
  • Character strings in Fortran: Portable LEN_TRIM and LNBLNK?
  • cant create handler inside thread that has not called looper.prepare
  • iPhone — MKReverseGeocoder.adminstrativeArea — getting state abbreviation
  • Unable to use dot layout (graphviz as a library)
  • How to read JSON-LD data from HTML in Objective-C?
  • iOS custom UTI in UIDocumentPickerViewController initWithDocumentTypes
  • reduce/reduce conflicts using ocamlyacc
  • Cannot connect to native local socket on android 5.1
  • Table striping rows in CSS Grid
  • Yii2: Using Kartik Depdrop Widget?
  • How to determine the CCSID used in CPYFRMIMPF command?
  • JavaScriptCore External Arrays
  • CFBundleDevelopmentRegion not works as expected
  • php is_file always return false
  • read values from form post in jquery or javascript
  • ZipList with Scalaz
  • apply a javascript function to draggable copy
  • Is it possible to run clang with llc flags
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)
  • abstracting over a collection
  • Clear activity stack before launching another activity
  • Problems installing Yesod for Haskell
  • How to revert to previous XCode version?
  • Webgrid not refreshing after delete MVC
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Jquery UI tool tip close icon
  • How to rebase a series of branches?
  • Knitr HTML Loop - Some HTML output, some R output
  • Where to put my custom functions in Wordpress?
  • QuartzCore.framework for Mono Develop
  • Numpy divide by zero. Why?
  • Traverse Array and Display in markup
  • Qt: Run a script BEFORE make