使用Xcode中的现有C代码为iOS创建静态库 [英] Make static library for iOS with existing C code in Xcode

查看:449
本文介绍了使用Xcode中的现有C代码为iOS创建静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的简短版本:

所以基本上我想做的是采用现有的用C语言编写的库( https://github .com/lsalzman/enet ),并将其转换为iOS的静态库.

So basically what I'm looking to do is to take an existing library written in C (https://github.com/lsalzman/enet) and turn it into a static library for iOS.

我只是想逐步了解如何在Xcode中完成此操作.

I am just looking for an easy to understand step by step of how to accomplish this in Xcode.

长版:

我已经看过一些教程,以制作用Objective-C(

I've gone over some tutorials for making a simple static library that's written in Objective-C (http://www.raywenderlich.com/41377/creating-a-static-library-in-ios-tutorial), and I generally understand what is happening there, but I'm failing to understand how to do this with existing code written in C.

我想我越来越近了,但是我不太确定.

I think I'm getting close, but I'm not so sure.

  1. 我首先在xcode中创建一个"Cocoa Touch静态库"项目.
  2. 我添加了所有enet .h和.c文件
  3. 确保enet内容位于构建设置的用户标题搜索路径"中.
  4. hit build-它可以编译!

生成的.a文件为517kb,所以我很确定它现在正在构建enet内容.

The generated .a file is 517kb, so I'm pretty sure it's building the enet stuff in at this point.

我现在的问题是该库的头文件基本上是空的:

My problem right now though is that the header file for the library is basically empty:

#import <Foundation/Foundation.h>

@interface enet_ios : NSObject

@end

我在想我要么需要在Objective-C中编写一个与enet库对话的包装器,要么需要以某种方式重新配置我的xcode项目,以便enet.h是该库的入口点",而不是xcode的预生成的.h/.m文件.不过,我不太确定该怎么做.理想情况下,我只想跳过任何包装程序,而使用enet库已经提供给我的东西.

I'm thinking I either need to write a wrapper in Objective-C that talks to the enet library, or I need to reconfigure my xcode project somehow so that enet.h is the 'entry point' into this library and not xcode's pre-generated .h/.m files. I'm not really sure how to do that, though. Ideally I'd just like to skip any sort of wrapper and use what the enet library is already providing me.

感谢您的光临!

推荐答案

问题,您是否正在尝试使用目标c语法/面向对象符号来调用函数?然后,您确实需要一个包装器对象,没有办法解决.

Question, are you trying to call the functions using objective c syntax / object orient notation? Then you do need a wrapper object, no way around that.

但是,如果您可以直接调用C函数,这在IOS/Objective C中是完全可以接受的,那么这就是确保您从enet库获得头文件(我在github中看到的include目录中的头文件)的问题您共享的链接)也随静态库一起分发.这是静态库的限制.您可以使用* .a复制它们,但必须使用静态库复制它们.这与包含.H和资产的Framework确实不同,该框架是开发人员不易使用Apple的IOS工具创建的.

But if you are fine calling C functions directly, which is completely acceptable in IOS/Objective C, then it is a matter of making sure your header files from the enet library (the ones in the include directory I see in the github link you shared) are also distributed with the static library. This is a limitation of the static library. You can copy them with the *.a, but they must be copied with the static library. This does differ from a Framework, which has included .H and assets, which developers are not easily able to create with Apple's tools for IOS.

我发现,出于这个原因和许多其他原因,使用Objective C进行库管理很痛苦,而静态库却是一个难题.另一个建议是将项目建设为cocoapod,这绝对是更优雅,更便携,但对于个人使用而言却有些过高的建议.您可以通过分叉项目并将其转换为共足动物来实现.有很多示例说明项目结构应如何在共足动物和其他OSS(如AFNetworking)上显示.这似乎是人们创建IOS库的事实上的标准方法.有关更多详细信息,请参见 http://cocoapods.org/.这将包括作为Pod的源代码,并针对目标应用程序进行编译.

I find that library management with Objective C to be painful on its own and static libraries a challenge for this and many other reasons. One more suggestion, definitely more elegant and portable but slightly overkill for personal use, would be building the project as a cocoapod. You can do this by forking the project and converting it to cocopods. There are lots of examples of how the project structure should look on cocopods and other OSS like AFNetworking. This seems to be the defacto standard way people are creating IOS libraries. See http://cocoapods.org/ for more details. This will include the source code as the pod and compiled against the target application.

这是我处理自己的图书馆和第三方图书馆的唯一方法.到目前为止,如果该库不使用cocoapods,我就不会使用该库或对其进行分叉,而是自己将其设置为pod....

This is the only way i deal with my own libraries and third party libraries. It has gotten to the point that if the library doesn't use cocoapods, i don't use the library or fork it and do make it a pod myself....

这篇关于使用Xcode中的现有C代码为iOS创建静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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