11

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

A_toaster
  • 1,196
  • 3
  • 22
  • 50
  • As you are on a mobile/connected device, you can run the calculation serverside and have just the UI on the device itself. This approach has the additional advantage of more computing power, user storage, multi-device access and other things that go with server side solutions. – transistor Mar 22 '17 at 07:35
  • Yeah that is definitely an option, and thats how I’m currently doing it, but the big disadvantage is that it can take a few seconds, which is why upon trying to get it done all locally – A_toaster Mar 22 '17 at 18:20
  • This will probably take some understanding of SPICE's source code to do. It is usually not trivial to make the funcionality of a command-line program available as a library. – Pedro Castilho Mar 23 '17 at 13:58
  • @PedroCastilho ah okay, I was under the impression that there may be an easy-ish way to do it since I could run the executable on OSX without any dependencies. Thanks regardless – A_toaster Mar 23 '17 at 23:01
  • Have you looked at the Ngspice [manual chapter about shared library compilation](http://ngspice.sourceforge.net/docs/ngspice-manual.pdf#chapter.19)? Other than that, have you at least tried static compilation, if so what problems have you encountered while using? – AndreLDM Mar 24 '17 at 14:49
  • I don't understand. You have fully completed source code written in C and you need a library? Why? You can use the uncompiled source code in your code using Objective C and if you are using Swift, look there at [Importing Objective-C into Swift](https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html). – Samuel Tulach Mar 25 '17 at 16:21
  • @AndreLDM Yeah, I've gone over the manual and tried to compile it under Mac OS, but I seem to have an issue with bison when running the `make` command. It says there are a bunch of syntax errors which I take to mean it requires restructuring on this platform. @SamuelTulach I guess for some reason I thought it would be easier to use this as a library in Swift, so that I wouldn't have to bridge it over. My thinking was that a UNIX executable could somehow be tweaked to allow it to be built as an iOS compatible library, although I'm now realizing that is not the issue! – A_toaster Mar 26 '17 at 06:42

1 Answers1

1

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

Community
  • 1
  • 1
acolchagoff
  • 1,926
  • 3
  • 18
  • 30