Use a custom C++ library in a Swift iOS project - ios

I have a C++ lib as a separate project that I'm trying to use in a Swift iOS app.
The approach I'm pursuing right now is:
Compile the lib into .dylib. This happens outside of Xcode.
In Xcode, add the lib to Build Phases -> Link Binary With Libraries.
Add the (C compatible) header to Bridging-Header.h.
Now, build succeeds but it crashes on simulator (something like ".dylib not found"), because the .dylib is not added to the app package.
Unfortunately I couldn't add the .dylib to "Embedded Binaries". I guess it must be somehow packaged into a framework (no idea how)?
How to make this work? Is including the sources into the project a better approach?

Related

How do I build native C source code to create a library for Unity, to be used for iOs Platform?

I want to import a library from native C source code to be used Unity, for iOs. I expect to require the .a binaries and the .h header (and any other file required if I'm missing any), but I cannot find any guide around about how to build it.
I tried to build the source code directly on Xcode, but I was not able to create a library from that. XCode accepts native C only with command line project, while I need a library.
Then I tried to run this CMake command on mac terminal:
cmake . -DCMAKE_SYSTEM_NAME=iOS "-DCMAKE_OSX_ARCHITECTURES=arm64;arm64e" -DCMAKE_OSX_DEPLOYMENT_TARGET=9.3 -DENET_STATIC=1 -DENET_LZ4=1 -GXcode
This creates an Xcode project, I build it for generic IOS but the code does not work on unity when I deploy it on the device.
A good place to start is the Bonjour example from the Unity Manual. This at least gives you a working iOS plugin as a starting point.
You include the files in your Unity project, and build for iOS. You will notice that the files under Plugins/iOS are automatically included in the generated Xcode project. They will only be compiled when you build/run your Xcode project.
Careful, an iOS plugin is not the same as a MacOS plugin. For a MacOS plugin, you will need to create the bundle and include this into your UnityProject. The DllImport decorator will also be different on your C# code. For iOS it is "__Internal", but for MacOS it is the name of the bundle.
If you are trying to interface with a third party library, you may need to manually modify the library search paths of your Xcode project to correctly locate your .a and .h files when building and linking.
As a side note, when including a third party .a binary, verify that it conforms to the iOS architecture requirements, otherwise your app may be rejected when submitting to the app-store.

Where to put a custom .dylib in a Swift app?

I'm trying to load a custom .dylib library via the dlopen() function (in a Swift iOS app). It requires a path to the library.
Where to put the library in the project structure?
What will be the library's path on the iOS device?
Also, a tangential question, it seems more usual to include the library's sources directly into the project and let Xcode build it. Is that a preferable approach for some reason?
1 )
If you include the .dylib library in with your list of files and resources in your project, you can use the "Copy Files" build phase to copy the dylib into your shipping app.
A tutorial can be seen here.
2 )
For dlopen, try using just the .dylib name before you try to use relative paths including the "#executable_path" or "#rpath" run time variables. An example can be seen here in this related question.
As for why some developers prefer to include the library source code in a project, I believe it's mostly just a preference by folks who aren't comfortable with the "Copy Files" build phase or in using dlopen.

How do I compile a library in Xcode using a makefile?

I need to compile a library written in C in Xcode. For this I need to use a make file. How can I include a make file in my project?
Any link for writing a make file or sample of a make file for running on the iOS simulator would be helpful.
Also if I use cmake, then what commands do I use in the terminal to create a static Library for iOS simulator?
Thanks
First, your project generator is either Xcode or Make. You can't have a makefile in Xcode.
If you want to generate an iOS library from C/C++ sources, take a look at this google project. The project wiki explains how to use the iOS CMake toolchain. This will give you a .xcodeproj file. You can build and then link to that library from other iOS projects. Also there's this fork available on Github which you could take a look at.
If the target system is exclusively iOS, you could alternatively create a new iOS library project from scratch (no CMake) and throw in your sources manually.

Compile FFmpeg as iOS 8 Framework

I successfully compiled FFmpeg with iOS 8.2 SDK thanks to https://github.com/kewlbear/FFmpeg-iOS-build-script and last version of gas-preprocessor (https://github.com/libav/gas-preprocessor).
However, I would like to package FFmpeg libraries as a iOS 8 dynamic framework due to legal constraints. I found resources to create iOS 8 dynamic framework however I cannot find any solution for FFmpeg.
Can anyone help me to package these librairies ?
Thanks
David
As far as I know, FFmpeg-iOS repo in Github can build static libraries from FFmpeg source code. But I search throughout the network, no one show me how to compile with dynamic libraries.
But I wonder if we can create a new cocoa touch framework project, and drag all header files and libraries into this project, and do some header declaration into the base .h file, and drag the framework project into an existing iOS project as a sub project, add it as an embedded framework, and compile the whole project.
The reason why I use sub project, instead of giving out a final .framework file, is that static symbols can only be linked only if them are been using somewhere.
I will demonstrate this later. If anyone has better ideas, it will be grateful.
Edit:
After several days's researching, I found it is not easy to build dynamic framework easily, but I find a workaround to achieve the target:
Build a static libraries of FFmpeg
Create a new iOS dynamic framework project
Create a class that encapsulate the basic usage of the FFmpeg, such as encoding/decoding video
Copy static libraries into this dynamic framework project
Make sure your project build without error
Add this project as a subproject to your existing project
Add dependency in embedded binaries and Linked Frameworks and Libraries
Build and run main project
Open source this project as LGPL2.1+, the same as FFmpeg itself.
Through it is not perfect, but at least it works, and it complies with FFmpeg's LGPL license.

How do I add this C library to my iOS Xcode 4 project?

I'm trying to add the openjpeg library to my XCode 4 project so that I can compress images taken by the iPhone's camera to jpeg2000.
I built the static library (libopenjpeg.a) using Cmake on OS/X. (I'm guessing this may have been the first error, that it needs to be built by XCode so it's built for iPhone architecture and not OS X).
I have the library added in the Link Binary with Libraries of my target.
The project builds successfully but I can't seem to import any of the headers from the library into any of my Objective-C classes. I've tried manually adding the folder that contains the libopenjpeg header files to the User Header Search Path but that did not seem to do anything.
Any suggestions?
for the simplest solution
Import the head files to you project's source.
You can still build it on the command-line with CMake, you'd just have to modify the CMakeLists.txt file so the right flags are passed when compiling.
However as Gavin indicates, it may be simpler just to drag the header and source files from the library into your Xcode project, and forego the building of a static library.

Resources