且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为ios应用程序构建和使用C ++静态库

更新时间:2023-10-16 10:05:10

这样做会

1)使用相同的方式创建c ++库,即iOS-> Framework& Library-> Xcode 6中的Cocoa Touch静态库.

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)构建保持配置iOS设备的静态库,然后构建iPhone 6(基本上是模拟器).

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

3),然后在文件浏览器"视图中展开产品".选择您的.a文件,说出libTestStaticLibrary.a,然后选择右键>在Finder中显示.在文件夹中上移.您应该能够看到两个Debug-iphoneos和Debug-iphonesimulator

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)现在打开终端,转到产品"路径,然后输入

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

lipo -create Debug-iphoneos/libTestStaticLibrary.a Debug-iphonesimulator/libTestStaticLibrary.a-输出libTestStaticLibrary.a

5)现在打开使用该库的项目,您需要拖放静态库文件以及具有静态库函数的函数声明的头文件.

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)现在,创建Cocoa touch类文件,该文件将充当静态库和目标-c文件之间的适配器.将扩展名更改为 .mm

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)现在在任何目标c文件中使用此MyCustomAdaptor.

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