xcode 如何为 ios 应用程序构建和使用 C++ 静态库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18811072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to build and use a C++ static library for ios application
提问by feelfree
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:
我知道如何使用iOS->Framework&Library->Cocoa Touch Static Library
xcode 4.6构建对象 C 静态库,在本教程的帮助下创建静态库在 iOS 教程中很简单。但是,我不确定的一件事是如何为 io 应用程序构建和使用纯 C++ 静态库。对于C++静态库的构建,我也是使用 iOS->Framework&Library->Cocoa Touch Static Library
guideline,不同的是我在创建静态库项目的时候把所有的.h和.m文件都删除了,然后把所有的C++静态库头文件和实现文件都放到了项目中. 一个非常简单的例子如下:
hello.h
你好.h
#include <iostream>
void say_hello();
hello.cpp
你好.cpp
#include "hello.h"
void say_hello()
{
std::cout<<"hello"<<std::endl;
}
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:
看起来很有效,我可以hello.a
为 iPhone 6.1 Simulator构建静态库。下一步是构建一个将调用静态库的应用程序。我iOS application->Single View Application
为 iPhone 6.1 Simulator构建了一个简单的模拟器,然后尝试使用以下代码简单地调用文件中的 hello.a
静态库ViewController.mm
(将 ViewController.m 更改为 ViewController.mm 以便它可以调用 C++ 函数):
say_hello();
However, I received one warning and two error messages:
但是,我收到了一条警告和两条错误消息:
Warning:
警告:
ld: warning: ignoring file hello.a, file was built for archive which is not the architecture being linked (i386):
Error 1:
错误 1:
hello.a
Undefined symbols for architecture i386:
"say_hello()", referenced from:
-[ViewController viewDidLoad] in ViewController.o
Error 2:
错误 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:
然后我有几个与这个实验相关的问题:
- 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?
- 这是创建纯 C++ 静态库的正确方法吗?
我调用 C++ 静态库的方式有问题吗?
在我的示例中,在调用静态库时,如何解决链接错误?
Many thanks.
非常感谢。
回答by vkhemnar
This will do,
这会做,
1)Create c++ library using same way, iOS->Framework&Library->Cocoa Touch Static Library in Xcode 6.
1)在Xcode 6中使用相同的方式创建c++库,iOS->Framework&Library->Cocoa Touch Static Library。
TestCPlusPlus.h
TestCPlusPlus.h
int sub(int a, int b);
TestCPlusPlus.cpp
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.)
2)构建静态库,保持配置iOS设备,然后是iPhone 6(基本上是模拟器。)
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
3) 然后在文件浏览器视图中展开产品。选择你的 .a 文件,说 libTestStaticLibrary.a ,然后右键 > 在 Finder 中显示。在文件夹中向上移动。您应该能够看到两个文件夹 Debug-iphoneos 和 Debug-iphonesimulator
4) now open Terminal go till this Products path then type
4)现在打开终端直到这个产品路径然后输入
lipo -create Debug-iphoneos/libTestStaticLibrary.a Debug-iphonesimulator/libTestStaticLibrary.a -output libTestStaticLibrary.a
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.
5) 现在打开你使用这个库的项目,你需要拖放静态库文件以及包含静态库函数函数声明的头文件。
6) Now, create Cocoa touch class file which will act as adaptor between static library and obejective -c files. Change the extension to .mm
6) 现在,创建 Cocoa touch 类文件,它将充当静态库和 obejective -c 文件之间的适配器。将扩展名更改为.mm
MyCustomAdaptor.h
我的自定义适配器.h
@interface MyCustomAdaptor : NSObject
-(int)getSub:(int ) a SecondParam:(int) b;
@end
MyCustomAdaptor.mm
我的自定义适配器.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.
7) 现在在任何目标 c 文件中使用此 MyCustomAdaptor。
回答by LeverkusenFan
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
请注意您的 .a 是使用 i386 或 armv7 构建的吗?通常,您应该构建两个版本并将它们合二为一。像这样: lipo -create -output libopencore-amrwb.a libopencore-amrwb-armv7.a libopencore-amrwb-i386.a
回答by T.V.
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".
当你构建你的库时,你必须记住你将在哪里使用它,iOS 设备或模拟器。这很重要,因为您必须针对不同的情况进行构建,这非常简单,构建库时只需选中“选择方案”即可。
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!
构建完成后,只需将创建的文件拖放到要使用库的项目中,就可以开始了!