Automatic
Automatic

Reputation: 70

Can I compile C++ library into dylib and use them in iOS 9 project?

I have an iOS app, which use liblzma. I am currently able to successfully write cross platform compile script to compile the source code into a .a file, and use the .a with headers in my project.

Now in iOS 8 and above, Apple start to support dynamic linking, so I was curious to see if I am able to use liblzma.dylib in my project instead of liblzma.a. But I failed.

Here's what I have done:

  1. modify the compile script I previously used to compile liblzma.a. I changed --disable-shared to --enable-shared, and then use lipo to combine .dylib of each arch to a fat .dylib.

  2. I removed the liblzma.a from my project, and added liblzma.dylib to my project

  3. Build, success. But when running on device, I encountered the following error

dyld: Library not loaded: /Volumes/Data/Developer/build/xz-5.2.2/build/iOS/arm64/lib/liblzma.5.dylib Referenced from: /var/containers/Bundle/Application/AB7A063F-9C3F-4CEB-A757-965238947000/****.app/**** Reason: Incompatible library version:**** requires version 8.0.0 or later, but liblzma.5.dylib provides version 6.0.0 (lldb)

  1. I used tool to check the actual version of the newly built .dylib, this is what I got

/Volumes/Data/Developer/build/xz-5.2.2/build/iOS/Universal/lib/liblzma.5.dylib: /Volumes/Data/Developer/build/xz-5.2.2/build/iOS/x86_64/lib/liblzma.5.dylib (compatibility version 8.0.0, current version 8.2.0) /usr/lib/libSystem.dylib (compatibility version 1.0.0, current version 1226.10.1)

I have a few questions:

Thanks!

Upvotes: 0

Views: 2069

Answers (1)

Automatic
Automatic

Reputation: 70

Solved!

  • Yes, i could load dylib like that
  • I need to add copy file build phase for the dylib to be copied
  • when liblzma.dylib is not copied, iOS seems to use a liblzma.dylib already on device, which has lower version number than required
  • I need to modify install name of the dylib using install_name_tool

Upvotes: 0

Related Questions