How to build and use a C++ static library for ios application

孤人 提交于 2019-12-04 03:14:15

This will do,

1)Create c++ library using same way, iOS->Framework&Library->Cocoa Touch Static Library in Xcode 6.

TestCPlusPlus.h

int sub(int a, int b);

TestCPlusPlus.cpp

int sub(int a, int b)
{
 return a - b;
}

2) Build the static library keeping configuration iOS Device, then iPhone 6(Basically simulator.)

3) then Expand Products in File Browser view. Select your .a file, say libTestStaticLibrary.a , then Right Button > Show in Finder. Move up in folders. You should be able to see two folers Debug-iphoneos and Debug-iphonesimulator

4) now open Terminal go till this Products path then type

lipo -create Debug-iphoneos/libTestStaticLibrary.a Debug-iphonesimulator/libTestStaticLibrary.a -output libTestStaticLibrary.a

5) Now open your project which uses this library, you need to drag and drop the static library files as well as the header files which have function declaration of static library functions.

6) Now, create Cocoa touch class file which will act as adaptor between static library and obejective -c files. Change the extension to .mm

MyCustomAdaptor.h

@interface MyCustomAdaptor : NSObject

-(int)getSub:(int ) a SecondParam:(int) b;

@end

MyCustomAdaptor.mm

#import "TestCPlusPlus.h"

@implementation MyCustomAdaptor

-(int)getSub:(int ) a SecondParam:(int) b
{
 int c = sub(a,b);
 return c;
}

@end

7) now use this MyCustomAdaptor in any of the objective c- file.

Please notice that your .a is build with i386 or armv7? Generally, you should build both versions and combine them into one. like this: lipo -create -output libopencore-amrwb.a libopencore-amrwb-armv7.a libopencore-amrwb-i386.a

I'm currently doing the same as you. I've had the same problem you describe here, actually the same two errors.

When you build your library you have to have in mind where're you going to use it, iOS device or simulator. This is important because you have to build for the different cases, this is very simple, when you build you library just check the "Select Scheme".

For Real device use:

Just to test in the simulator use:

After building just drag-drop the files created to the project you want to use the library and you're good to go!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!