2

Short version of the question:

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.

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

Long version:

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. I start out by making a "Cocoa Touch Static Library" project in xcode.
  2. I add all of the enet .h and .c files
  3. make sure the enet stuff is in my "User Header Search Paths" in build settings.
  4. hit build - it compiles!

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

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.

Thanks for taking a look!

  • Go to "Build Phases" and add a Copy Headers (not copy files) build phase, and add enet's headers to the public headers. You will also have to configure your public headers folder path in the build settings - this is where Xcode will put the headers to go with the library. – quellish Jul 21 '14 at 08:11
  • How did you fare with this? – Engineer Jul 24 '15 at 15:04

1 Answers1

1

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.

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.

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.

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....

Andrew Phillips
  • 937
  • 1
  • 7
  • 19
  • Thanks for the tips! Regarding including the enet header file, would that just be a matter of copying/pasting the .h file into the include folder that's generated alongside the .a file? – robochase6000 Jul 17 '14 at 16:56
  • I'm totally fine calling the C functions directly. In fact, I think I have to for what I'm doing. The end goal here is to get the enet library working with Haxe on iOS. I've got Haxe talking to enet (as a dylib) on Mac, I'm just having some problems figuring out how to set up my libraries for iOS – robochase6000 Jul 17 '14 at 16:58
  • Yes, sorry, should have been clear on that. You can put them in the include directory as shown in the tutorial you reference. Just to double check, i made a quick test project and a example C H file, copied it there, and it stayed even when i did a "clean" and "build" so it should stay there. The down side is, you now have two copies of the same file since the compiler isn't nice enough to copy it for you. But i think that is unavoidable with C header in static library. – Andrew Phillips Jul 17 '14 at 17:01
  • alrighty, i'm sure you could probably write up some copy/paste script as part of the build process, but i'm totally fine doing it by hand for this one thing. I'll try this out tonight, I hope it works! Thanks again :) – robochase6000 Jul 17 '14 at 17:31
  • ok, so it's not exactly working, but the bounty is yours! I was able to compile a static library, and I was able to confirm that it's at least linking my static library correctly. my current problem is that it's not finding some 'primitives', which are declared in a .cpp that i'm trying to expose. At this point, I'm not sure whether it's a problem with the way I'm compiling my library, or if it's some syntactical issues with haxe or something. Thanks for your help, I think I've gotten just a bit closer to what i'm trying to accomplish. – robochase6000 Jul 21 '14 at 10:20
  • You might be able to throw some externs into the header file (e.g. extern int timer)... have you tried that? If you give me a couple of examples, i can see if i can help... – Andrew Phillips Jul 21 '14 at 19:02