Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I would like to use a UNIX command-line program (Berkeleys SPICE) in an iOS app. What is the process to compile it into a usable library?

I am trying to use Berkeley's SPICE tool in an iOS app, but am having trouble compiling it for iOS.

It is a command-line program that I can call from a terminal like:

./spice3f5 <arguments>

Which works well, and I would like this functionality in my iOS app, but I don't think I can just copy the executable over to Xcode and call it from Swift.

I've done some research and found the following:

  • There is an updated version of SPICE called ngspice, which is relatively new (2014 release)
  • I'm fairly sure there are apps out there than have used either SPICE or ngspice, so I'm sure it can be done somehow.
  • I have read an article about a guy who says that ngspice has been compiled as a shared library(ctrl+f "ngspice"), and he made an app with it. I have emailed him but he unfortunately he has not responded.

The reason I am asking here is because when googling for "ngspice iOS", I came across this thread which has a lot of smart people trying to compile a static library, which seems way out of my scope. I learned that dynamic libraries are allowed as of iOS8. So would it be easier to compile a *.dylib than it is a static library?

How would I goabout using ngspice or SPICE in an iOS app?

Thanks

like image 953
A_toaster Avatar asked Mar 14 '17 07:03

A_toaster


1 Answers

The difference between a static and a dynamic library is essentially where they live, a static library will live inside the binary of your app, and an dynamic library will live on the system (iPhone) that runs your app. there isn't much difference as far as difficulty goes. If you wanted to go the dynamic route on os x for example, you might compile a .dylib file in a separate project first. Then copy your new .dylib file into /usr/lib or a similar location that is part of your system's path. Then you would need to copy the associated header files that know how to talk to your new .dylib file into your /usr/include folder. At this point you could import said header files in xcode using angle brackets like so:

#import <my_dylib_header_file.h>

in static world however, you would simply drag your .dylib file into xcode then copy the associated header files into your source folder and then import using quotes like so:

#import "my_dylib_header_file.h"

the advantage of doing the import statically is that the library becomes baked into your final product, as opposed to a dynamic link, which will require that the dylib is installed on the system prior to the binary being able to run properly (think DLL's on windows). The disadvantage of a static import is that the final binary is larger, as it contains more code.

The advantage of a dynamic import is that the binary is smaller, and dylib can be updated without updating the binary itself.

However based on your questions I don't think any of this matters for your project. You have the source code. Which means creating a dylib is entirely unnecessary for your purpose, you can treat the source code like a static library by simply adding it to your xcode project. If I were you I would add the spice source code to my xcode project and forget about creating a dylib. From there I would import the files and make calls to them from swift. There are lots of threads out there that explain how call c functions or objective-c classes from swift so I wont go into that here, instead I'll refer you to another answer: Swift: How to call a C function loaded from a dylib

like image 64
denodster Avatar answered Oct 20 '22 03:10

denodster