如何为ios应用程序构建和使用C ++静态库 [英] How to build and use a C++ static library for ios application

查看:74
本文介绍了如何为ios应用程序构建和使用C ++静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用xcode 4.6中的 iOS-> Framework& Library-> Cocoa Touch静态库构建对象C静态库,在本教程的帮助下,它很简单在iOS教程中创建静态库.但是,我不确定的一件事是如何为io应用程序构建和使用纯C ++静态库.为了构建C ++静态库,我还使用了 iOS-> Framework& Library-> Cocoa Touch静态库准则,不同之处在于,我在删除所有.h和.m文件时创建静态库项目,然后将所有C ++静态库头文件和实现文件放入项目中.一个非常简单的示例如下:

I know how to build a object C static library using iOS->Framework&Library->Cocoa Touch Static Library in xcode 4.6, and it is straightforward with the help of this tutorial Creating a Static Library in iOS Tutorial. One thing I am not sure, however, is how to build and use a pure C++ static library for io application. For building a C++ static library, I am also use iOS->Framework&Library->Cocoa Touch Static Library guideline, and the difference is that I delete all the .h and .m files when creating the static library project and then put all the C++ static library head files and implementation files in the project. A very simple example is as follows:

hello.h

#include <iostream>
void say_hello();

hello.cpp

hello.cpp

#include "hello.h"

void say_hello()
{
std::cout<<"hello"<<std::endl;
}

这似乎可行,我可以为iPhone 6.1 Simulator构建 hello.a 静态库.下一步是构建将调用静态库的应用程序.我为iPhone 6.1模拟器构建了一个简单的 iOS应用程序->单视图应用程序" ,然后尝试在 ViewController.mm <中调用 hello.a 静态库./code>文件(将ViewController.m更改为ViewController.mm,以便它可以调用C ++函数),只需使用以下代码即可:

It seems working, and I can build hello.a static library for iPhone 6.1 Simulator. The next step is to build an application that will invoke the static library. I build a simple iOS application->Single View Application for iPhone 6.1 Simulator, and then try to invoke the hello.a static library in ViewController.mm file (change ViewController.m to ViewController.mm so that it can invoke C++ function) simply with the following code:

say_hello();

但是,我收到了一条警告和两条错误消息:

However, I received one warning and two error messages:

警告:

ld: warning: ignoring file hello.a, file was built for archive which is not the architecture being linked (i386): 

错误1:

hello.a
Undefined symbols for architecture i386:
  "say_hello()", referenced from:
      -[ViewController viewDidLoad] in ViewController.o

错误2:

ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

然后我有几个与此实验有关的问题:

I then have several questions related to this experiment:

  • 创建纯C ++静态库是正确的方法吗?
  • 调用C ++ static的方式是否有问题库?

  • Is it the right way to create a pure C++ static library?
  • Is there something wrong with the way how I invoke the C++ static library?

在我的示例中,当调用静态库时,如何解决链接错误?

In my example, when invoking the static library, how could I solve the link errors?

非常感谢.

推荐答案

这样做会

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.

这篇关于如何为ios应用程序构建和使用C ++静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆