fee*_*ree 10 c++ xcode objective-c static-libraries ios
我知道如何使用iOS->Framework&Library->Cocoa Touch Static Libraryxcode 4.6 构建一个对象C静态库,在本教程的帮助下,在iOS教程中创建一个静态库是很简单的.但是,我不确定的一件事是如何为io应用程序构建和使用纯C++静态库.为了构建C++静态库,我也使用 iOS->Framework&Library->Cocoa Touch Static Library指南,区别在于我在创建静态库项目时删除所有.h和.m文件,然后将所有C++静态库头文件和实现文件放在项目中.一个非常简单的例子如下:
hello.h
#include <iostream>
void say_hello();
Run Code Online (Sandbox Code Playgroud)
HELLO.CPP
#include "hello.h"
void say_hello()
{
std::cout<<"hello"<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
它似乎工作,我可以hello.a为iPhone 6.1模拟器构建静态库.下一步是构建一个将调用静态库的应用程序.我构建了一个简单iOS application->Single View Application的iPhone 6.1模拟器,然后尝试调用文件中的 hello.a静态库ViewController.mm(将ViewController.m更改为ViewController.mm,以便它可以调用C++函数),只需使用以下代码:
say_hello();
Run Code Online (Sandbox Code Playgroud)
但是,我收到一条警告和两条错误消息:
警告:
ld: warning: ignoring file hello.a, file was built for archive which is not the architecture being linked (i386):
Run Code Online (Sandbox Code Playgroud)
错误1:
hello.a
Undefined symbols for architecture i386:
"say_hello()", referenced from:
-[ViewController viewDidLoad] in ViewController.o
Run Code Online (Sandbox Code Playgroud)
错误2:
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
然后,我有几个与此实验相关的问题:
我如何调用C++静态库的方式有问题吗?
在我的例子中,当调用静态库时,我该如何解决链接错误?
非常感谢.
这样做,
1)在Xcode 6中使用相同的方式创建c ++库,iOS-> Framework&Library-> Cocoa Touch Static Library.
TestCPlusPlus.h
int sub(int a, int b);
Run Code Online (Sandbox Code Playgroud)
TestCPlusPlus.cpp
int sub(int a, int b)
{
return a - b;
}
Run Code Online (Sandbox Code Playgroud)
2)构建静态库保持配置iOS设备,然后iPhone 6(基本模拟器).
3)然后在文件浏览器视图中展开产品.选择你的.a文件,比如libTestStaticLibrary.a,然后选择右键>在Finder中显示.在文件夹中上移.你应该能够看到两个folers Debug-iphoneos和Debug-iphonesimulator
4)现在打开终端直到此产品路径然后键入
lipo -create Debug-iphoneos/libTestStaticLibrary.a Debug-iphonesimulator/libTestStaticLibrary.a -output libTestStaticLibrary.a
5)现在打开使用该库的项目,需要拖放静态库文件以及具有静态库函数的函数声明的头文件.
6)现在,创建Cocoa触摸类文件,它将充当静态库和obejective -c文件之间的适配器.将扩展名更改为.mm
MyCustomAdaptor.h
@interface MyCustomAdaptor : NSObject
-(int)getSub:(int ) a SecondParam:(int) b;
@end
Run Code Online (Sandbox Code Playgroud)
MyCustomAdaptor.mm
#import "TestCPlusPlus.h"
@implementation MyCustomAdaptor
-(int)getSub:(int ) a SecondParam:(int) b
{
int c = sub(a,b);
return c;
}
Run Code Online (Sandbox Code Playgroud)
@结束
7)现在在任何目标c文件中使用此MyCustomAdaptor.
小智 1
请注意你的.a是用i386还是armv7构建的?一般来说,您应该构建两个版本并将它们合并为一个版本。像这样: lipo -create -output libopencore-amrwb.a libopencore-amrwb-armv7.a libopencore-amrwb-i386.a