1

I have a C++ code and I am able to create .so file for Android using ndk-build

Similarly, for iOS, I intend to create .a (Universal Static library) from existing C++ code.

Question is how to build .a file with existing C++ code?

Ninad2110
  • 11
  • 2
  • How are you building your code? What have you tried? What problem did you encounter? Please show a [mre] – Alan Birtles Aug 03 '21 at 06:30
  • @AlanBirtles - I am trying to build C++ p7zip (portable version of 7zip for *nix like OS), I searched for examples there I see mostly mentioning with objective-c/Swift. I have xcode but I don't see Cocoa Touch static library – Ninad2110 Aug 03 '21 at 12:37
  • you can probably just run the make file with the appropriate compiler flags to build a universal library, if that doesn't work you can probably build two separate libraries and lipo them together – Alan Birtles Aug 03 '21 at 14:03

1 Answers1

0

In Xcode (starting with either one of the template iOS Application projects (or an existing one):

  1. Create a new static library target: File -> New -> Target.... Select Framework Or Library, and then Cocoa Touch Static Library
  2. Add library source code: Drag library source code into Xcode project. In the dialog that appears, select the build target created above.
  3. Add project dependency to the library: Select Project in Project Navigation, the the iOS build target from Targets. Select the Build Phases tab, then under Target Dependencies in the window, click on the + sign. A sheet opens (choose items to add) and the library target should be at the top of the list.
  4. Include Library in iOS Target: Under Link Binary with Libraries, click +. The library (a .a file) should be at the top of the list.
  5. Link with libc++: As the step above. Select libc++ from the list.
  6. Enable Objective-C++ compilation for any source code file that needs to include the library headers by changing the extension from .m to .mm
  7. Build the iOS application target.

Xcode will have taken care of setting everything else up for you, including compiler flags and header search paths.

Shuduo
  • 727
  • 5
  • 14