使用C ++静态库编译错误包含在Swift项目中

时间:2014-08-03 15:57:09

标签: c++ ios objective-c swift static-libraries

我创建了一个包含以下C ++文件的静态库:

//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;
}

我添加了

-x objective-c++
“编译源”和

中的

标志

-lstdc++
“Info.plist其他预处理程序标志”中的

标志。

当我链接刚创建的静态库(使用Objective C包装文件)时,我收到4个跟随错误,我不知道如何解决它:

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)

我会很感激任何想法。

4 个答案:

答案 0 :(得分:3)

根据我的经验,当顶级项目中没有C ++源时,Xcode将无法链接到C ++库。在纯Obj-C项目中也是如此,您也可以链接到具有C ++代码的静态库。

一种侵入性较小的解决方案是在项目中简单地包含一个空的C ++源文件(这里是example cpp stub),它被“编译”到最终的.app二进制文件中。它实际上不会发出任何代码,但它使Xcode意识到存在C ++代码,导致C ++库被链接。

这个解决方案的优点是它避免了修改Xcode中的项目设置,所以不需要添加特殊的链接器标志来强制Xcode做正确的事情。

答案 1 :(得分:0)

These are the C++ settings for linking with the standard library

你应该有-x c ++而不是objective-c ++。但是,如果您命名C ++ sorce * .cpp。

,则无需指定此标志

答案 2 :(得分:0)

当我将“-lstdc ++”编译标志添加到Swift项目本身的“其他链接器标志”部分而不仅仅是在带有c ++文件的静态库项目中时,我修复了它。

答案 3 :(得分:0)

#ifndef __SEM_H__ #define __SEM_H__ #include <pthread.h> typedef struct test_semaphore tsem_t; tsem_t *tsem_new (int value); void tsem_free (tsem_t *sem); void tsem_wait (tsem_t *sem); int tsem_try_wait (tsem_t *sem); void tsem_signal (tsem_t *sem); #endif /* __SEM_H__ */ 应位于“其他链接标记”(或gcc sem.c dining.c -pthread -o dining )中。请注意,只有在“C ++标准库”设置为-lstdc++时才能使用。

另外值得注意的是,如果目标中包含任何C ++源代码,Xcode通常足够智能包含标准C ++库,因此您无需显式链接到OTHER_LDFLAGS或{{1 }}

相关问题