Mac Xcode4 怎么把静态库.a加到工程,并调用静态库中的函数

2025-05-01 05:03:49
推荐回答(1个)
回答1:

1、在Xcode中新建一个Project.选择如下图:

2、然后点next,下一步至于填写的东西随意。

创建完成后结构如下图:

3、未生成的静态库在Products下为红色,

然后随便在.h文件中写一个简单的方法sayHello;

#import

@interface TestStaticeLibrary : NSObject

-(void)sayHello;

@end

.m文件内容:

#import "TestStaticeLibrary.h"

@implementation TestStaticeLibrary

-(void)sayHello{

    NSLog(@"Hello OSChina");

}

@end

由于模拟器是采用i386模式进行开发的如果要在模拟器中使用静态库的话需要把iOS Device改成iphone 6.0 Simulator.然后点击run,就会生成.a文件。

引用方法:

创建任意IOS project,把刚才生成的静态库直接拖到新的项目中,结构如下:

如果只是单单加入静态库是不够的,之前生成的还有一个文件夹include,里面还带了一个.h文件,把.h文件引入项目中,最终目录结构如下:

如果不是直接拖入项目中的话可以,鼠标右键在项目中,选择ADD Files to "XXXX"

在ViewController 敲入如下代码:

#import "ViewController.h"

#import "TestStaticeLibrary.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
   [super viewDidLoad];
   
   TestStaticeLibrary *t = [[TestStaticeLibrary alloc] init];
   [t sayHello];
   
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

@end

然后运行项目 在控制台看到以下内容