4

I remember in old Xcode there was option under Create New Project there was option to create BSD Dynamic Library - dylib. However now I see only Cocoa Framework, Static Library and Metal Library.

Any hints?

clemens
  • 16,716
  • 11
  • 50
  • 65
  • Do you mean _macOS_? Xcode never supported dylibs for iOS. – clemens Jan 11 '18 at 15:00
  • How come? iOS is using dylibs. For example /usr/lib/libnetwork.dylib –  Jan 11 '18 at 15:53
  • Yes, dynamic libs are possible in iOS, but Apple doesn't support custom dynamic libs. They make no sense, because you can't install them on a non-rooted device. – clemens Jan 11 '18 at 16:01
  • I see, what i have in mind is jailbroken device. How can i create/adapt project for such ? –  Jan 11 '18 at 16:07
  • You can create a static library project and convert it either to a dynamic one or create a dynamic one with a custom build phase. Creating a dylib for iOS works almost like on macOS / Linux. But why you are using no static lib? – clemens Jan 11 '18 at 17:46
  • It's simple MobileSubstrate tweak to Cydia. How to convert static lib to dynamic lib ? –  Jan 11 '18 at 18:23

1 Answers1

5

You may create a project for an iOS static lib, and create a dynamic lib from the static one with a custom build phase with the following command:

xcrun --sdk iphoneos clang -arch <ARCH> -shared -all_load \
    -o lib<NAME>.dylib lib<NAME>.a

where <ARCH> is either armv7 or arm64 and <NAME>is the base name of your lib. You may check or list the architectures with file command. E.g.:

file <path>/lib<NAME>.a`
lib<NAME>.a (for architecture armv7):   current ar archive random library
lib<NAME>.a (for architecture arm64):   current ar archive random library

If more than one architecture is listed, Xcode has produced a universal static library. In this case you may create a universal shared library with multiple -arch flags:

xcrun --sdk iphoneos clang -arch armv7 -arch arm64 -shared -all_load \
    -o lib<NAME>.dylib lib<NAME>.a

You may need to specify additional linker flags (e.g. -l for linking non-standard libs).

clemens
  • 16,716
  • 11
  • 50
  • 65