* * *

Author Topic: Questions about making iOS library.....  (Read 6062 times)

chenyuchih

  • Jr. Member
  • **
  • Posts: 54
Questions about making iOS library.....
« on: May 30, 2016, 10:33:50 am »
Hello,

I would like to write some libraries for iOS and I saw there's precompiled cross-compiler package in sourceforge. So I installed the following things sequentially:

Xcode (and the command line tool)
fpc-3.0.0.intel-macosx.dmg
fpc-3.0.1.intel-macosx.cross.ios.dmg
fpcsrc-3.0.0-20151207-macosx.dmg
lazarus-1.6-i686-macosx.dmg

My questions are
1. Does it mean that I can start to write the most basic program/library(no GUI, no framework) for iOS? If not, what should I need more?
2. How should I config the project options? (CPU-target as "arm-darwin" and "a64-darwin"?)
3. I tried to compile one library for iphone simulator(i386-iphonesim), but the compiler shows that unit system cannot be found. Where can I found the needed units?

Thanks for your help!

Best Regards,
ChenYuChih

Phil

  • Hero Member
  • *****
  • Posts: 2749
Re: Questions about making iOS library.....
« Reply #1 on: May 30, 2016, 09:13:23 pm »
1. Does it mean that I can start to write the most basic program/library(no GUI, no framework) for iOS? If not, what should I need more?

Do you actually mean "library" or do you mean "unit" or something else? Creating a program (app bundle) for iOS is different from creating a library (framework included in an app bundle) for iOS.

This is what "library" means:

https://dl.dropboxusercontent.com/u/28343282/MacXPlatform/PascalDynLibs.html

These articles don't deal specifically with iOS, but the principles are the same.

-Phil

chenyuchih

  • Jr. Member
  • **
  • Posts: 54
Re: Questions about making iOS library.....
« Reply #2 on: May 31, 2016, 02:01:21 am »
Well, the library what I mentioned is "dynamically loaded library"(*.dll, *.so, *.dynlib). What I want is to create a dynlib with some numerical and string processing for other iOS programs. Do I need any other packages or patches for this purpose?

Thanks.

ChenYuChih

Phil

  • Hero Member
  • *****
  • Posts: 2749
Re: Questions about making iOS library.....
« Reply #3 on: May 31, 2016, 02:13:24 am »
Very good. Yes, you can create a dynamic library for iOS with FPC. For example, with the ndfd example library in the article series above, you can compile it into a multi-architecture library for iOS like this (in a bash script):

Code: Pascal  [Select]
  1. #!/bin/sh
  2. set -e
  3.  
  4. fpc_options="-S2ha -CiroR -O2 -XX -Fu~/Tools/WST"
  5.  
  6. sdkroot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk
  7.  
  8. ppcarm -Cparmv7 -Cfvfpv3 $fpc_options -XR$sdkroot -olibndfd-arm ndfd.pas
  9. ppca64 $fpc_options -XR$sdkroot -olibndfd64-arm ndfd.pas
  10.  
  11. sdkroot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk
  12.  
  13. ppc386-3.0.1 -Tiphonesim $fpc_options -XR$sdkroot -olibndfd-sim ndfd.pas
  14. ppcx64-3.0.1 -Tiphonesim $fpc_options -XR$sdkroot -olibndfd64-sim ndfd.pas
  15.  
  16. lipo -create libndfd-arm libndfd64-arm libndfd-sim libndfd64-sim -output ndfd
  17. install_name_tool -id ndfd ndfd
  18. rm libndfd-arm
  19. rm libndfd64-arm
  20. rm libndfd-sim
  21. rm libndfd64-sim
  22.  

Note that libraries included with an iOS app bundle must be in a framework, hence why the library file created is named "ndfd" instead of the "libndfd.dylib". This ndfd library file is copied into a framework folder (eg, ndfd.framework) along with the framework's Info.plist file. Finally, ndfd.framework is copied into the app bundle's Framework subfolder.

Note that for distribution with an actual arm app (not Simulator) you can use lipo to strip out the i386 and x86_64 architectures' code. For testing with the Simulator, it doesn't hurt to leave the armv7 and arm64 architectures' code in the library, even though the arm code will not be used under the Simulator.

-Phil

chenyuchih

  • Jr. Member
  • **
  • Posts: 54
Re: Questions about making iOS library.....
« Reply #4 on: May 31, 2016, 10:48:11 am »
Thanks Phil,

Sounds that the released version (3.0.0 & 3.0.1 cross-compiler) is enough for my usage.

But I'm still curious about how to work with Lazarus 1.6, because I do like the Lazarus IDE...

Is there any response to my 2nd and 3rd question?

BTW, I saw you specify the SDK path in the script, where should I set it in Lazarus? (It seems not the same as Android library path...)

Thank you very much!

ChenYuChih

Phil

  • Hero Member
  • *****
  • Posts: 2749
Re: Questions about making iOS library.....
« Reply #5 on: May 31, 2016, 10:31:18 pm »
Is the Pascal library you're developing specific to iOS or can it be compiled for, say, OS X too. If it's not iOS-specific (for example, does not depend on any iOS frameworks, only FPC RTL), then you can probably do the development with Lazarus, per usual.

A good way of debugging Pascal library code is to compile it into a standalone program in addition to a library. Then you can debug the program per usual as well. For example, in the ndfd example library from the article series above, the source includes a testndfd.pas program that just compiles in the NdfdForecast.pas unit rather than loading the library.

If your library is iOS-specific, then I would not use Lazarus, except perhaps as a code editor. The development of the iOS app that uses the library would likely be done with Xcode, so that's where I would work as much as possible.

-Phil

chenyuchih

  • Jr. Member
  • **
  • Posts: 54
Re: Questions about making iOS library.....
« Reply #6 on: June 01, 2016, 03:24:07 am »
Is the Pascal library you're developing specific to iOS or can it be compiled for, say, OS X too. If it's not iOS-specific (for example, does not depend on any iOS frameworks, only FPC RTL), then you can probably do the development with Lazarus, per usual.

A good way of debugging Pascal library code is to compile it into a standalone program in addition to a library. Then you can debug the program per usual as well. For example, in the ndfd example library from the article series above, the source includes a testndfd.pas program that just compiles in the NdfdForecast.pas unit rather than loading the library.

If your library is iOS-specific, then I would not use Lazarus, except perhaps as a code editor. The development of the iOS app that uses the library would likely be done with Xcode, so that's where I would work as much as possible.

-Phil


Wow...What a exciting message! Yes, the library I want to make is non-iOS specific. It's just some engineering algorithm, iteration, calculation, and lot of data(in string). I've maintained it on MS Windows and Android with exact the same code for 2 years and now I want to try it on iOS/OS X without too much modification.

About the GUI design, I would consider the Xcode for better co-working with other partners.

Thanks Phil! I DO APPRECIATE YOUR HELP!

Best Regards,
ChenYuChih

Phil

  • Hero Member
  • *****
  • Posts: 2749
Re: Questions about making iOS library.....
« Reply #7 on: June 01, 2016, 03:43:43 am »
I would start with getting the library working on OS X. That way if you have problems you'll know it's due to some difference between Windows and OS X and not something about iOS or arm.

If you can get it working with both 32-bit and 64-bit on OS X, you shouldn't have much trouble getting it to work with 32-bit and 64-bit Simulator and iOS.

I would also create a header file (.h) for the library. Programmers working with Xcode will likely be using Objective C or Swift. Both can use a C header file without any additional changes to it. For Swift programmers, you could even wrap the library functions in Swift classes to make the library easier to use - see the referenced article series for an example for the ndfd library (NdfdLib.swift contains the wrapper class and ndfd.h is the header file).

-Phil

chenyuchih

  • Jr. Member
  • **
  • Posts: 54
Re: Questions about making iOS library.....
« Reply #8 on: July 11, 2018, 12:40:28 pm »
Hello,

Sorry for continuing this thread because I don't have better topic for my new questions...

Recently I have time really start my work about porting my project to iOS and I found something strange. My environment is as follows:
Mac OS X 10.13.5
Xcode 9.4.1(and the command line tool)
fpc-3.0.4.intel-macosx.dmg
fpc-3.0.5.intel-macosx.cross.ios.dmg
fpcsrc-3.0.4-macosx.dmg
lazarus-1.8.4-i686-macosx.dmg

1. When I build my library, there's no pain for Darwin-i386, Darwin-x86_64, Darwin-arm, and Darwin-aarch64, all with default target processor. But I noticed that those generated dylib files seems the SAME!! They have complete the same file size!! Then I use the terminal command "file" to check them and the responses I got are all "Mach-O dynamically linked shared library i386". I didn't change any path parameters or special setups after installing fpc/lazarus. Did I miss something else? If so, why there's no error during the whole building process?

2. Another minor question is about the target processor setup. What's the default processor for Darwin-arm? I can build code with "(Default)" but not when changing it to others(ARMV3 to CORTEXM3, got "Error: Illegal parameter: -CpXXX"). I am curious about what processor will be used for default?

Thanks for help. Any response will be appreciated.

Sincerely,
ChenYuChih

chenyuchih

  • Jr. Member
  • **
  • Posts: 54
Re: Questions about making iOS library.....
« Reply #9 on: July 11, 2018, 04:56:35 pm »
Hi all,

I tried adding

-ap
-XR/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/
-FD/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/

in the Custom options, but got "Error: linker: Undefined symbols for architecture i386:" and fail generating dylib. Looks like the linker still want to link files as i386 even if the compiler option is "-Tdarwin -Paarch64". Is it a bug? Can I fix it manually?

Best Regards,
ChenYuChih

Phil

  • Hero Member
  • *****
  • Posts: 2749
Re: Questions about making iOS library.....
« Reply #10 on: July 11, 2018, 06:04:36 pm »
Hi all,

See example script above from a couple years ago.


chenyuchih

  • Jr. Member
  • **
  • Posts: 54
Re: Questions about making iOS library.....
« Reply #11 on: July 12, 2018, 04:28:47 am »
Hi all,

See example script above from a couple years ago.



Yes, I ran the script you showed (of course, the file path was changed to mine) and get similar result. The compiler went smoothly but still failed to link. This time I got the error message is

"ld: symbol dyld_stub_binding_helper not found, normally in crt1.o/dylib1.o/bundle1.o for architecture armv7"
"An error occurred while linking"

then just stopping. Is there version problem in crt1.o/dylib1.o/bundle1.o? How do I got correct one?

--
Edit:

Ok, after several tests I found that only armv7 has build problem as above. The iphonesim32/64 and a64 works! Thank you Phil.

So the left questions are:
1. How to successfully build the armv7 dylib?
2. Why Lazarus doesn't run correct script as Phil's one?
--

Thanks,
ChenYuChih
« Last Edit: July 12, 2018, 05:19:26 am by chenyuchih »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus