Compile errors with C++ static library include in

2019-07-09 03:40发布

问题:

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

-lstdc++

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.

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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++.