简体   繁体   English

将静态C ++库包含到Objective C项目中

[英]Include static C++ library to Objective C project

I want to create an C++ so library and include it in my Objective C code. 我想创建一个C ++ so库并将其包含在我的Objective C代码中。 I work in XCode. 我在XCode中工作。 Here is C++ code: 这是C ++代码:

--------------core.cpp---------- -------------- core.cpp ----------

#include <vector>
#include <algorithm>

extern "C" void my_sort(std::vector<int>& a) throw()
{
 sort(a.begin(), a.end()); // this is std::vector's sort function 
}

So I want to create the so library for including it in the Obejctive C code. 因此,我想创建一个so库,以便将其包含在Obejctive C代码中。 How to include or import it ? 如何包含或导入它? .. I want to call my_sort() function ? ..我想调用my_sort()函数吗?

THanks ! 谢谢 !

The trouble here is that your function has external C linkage. 这里的问题是您的函数具有外部C链接。 Hence it cannot use arguments of type std::vector , neither throw declarations as these are C++ stuff. 因此,它不能使用std::vector类型的参数,也不能throw声明,因为它们是C ++的东西。

To include it into your objective C code, you have to write the accompanying header file, that will declare your exported function. 要将其包含在目标C代码中,必须编写随附的头文件,该文件将声明导出的函数。 It will be much easier to export it once you will have removed all references to C++ from its interface. 一旦从其接口中删除了对C ++的所有引用,将其导出将更加容易。 It will also avoid the need to include the C++ headers. 它还将避免需要包含C ++头文件。

Then, to use it from your Objective C code, #include your header file, and give the linker information to your library. 然后,要从您的Objective C代码中使用它,请#include您的头文件,并将链接器信息提供给您的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM