Include static C++ library to Objective C project
I want to create an C++ so library and include it in my Objective C code. I work in XCode. Here is C++ code:
--------------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. How to include or import it ? .. I want to call my_sort() function ?
THanks !
The trouble here is that your function has external C linkage. Hence it cannot use arguments of type std::vector
, neither throw
declarations as these are C++ stuff.
To include it into your objective C code, you have to write the accompanying header file, that will declare your exported function. It will be much easier to export it once you will have removed all references to C++ from its interface. It will also avoid the need to include the C++ headers.
Then, to use it from your Objective C code, #include your header file, and give the linker information to your library.
精彩评论