* * *

Author Topic: Mobile dev.: Lazarus or Xamarin?  (Read 4663 times)

tk

  • Sr. Member
  • ****
  • Posts: 354
    • tkweb
Mobile dev.: Lazarus or Xamarin?
« on: October 15, 2016, 08:06:26 pm »
Hi all!
Since I am still quite a rookie with regard to mobile dev. (only made cosmetic modifications to an existing Android app in Java) I would like to ask if Lazarus is already mature enough to start developing mobile app in:

1. The app should be simple visualization for our controllers (4 screens + some configuration of the HTTP/UDP connection,no dbs,simple ui,simple charts).
2. Existing codebase in Pascal should be reused (implements our communication protocols etc.)
3. Single source code (small UI differences acceptable).
4. Must run in Android,iOS (WP is not desired, but would be an advantage).
5. Easy IDE setup, app deployment and maintenance.

To me, most viable way now is to use Xamarin (should be free now at least for simple apps), but problem is point 2. That's why I would like to use Lazarus or any other suitable pascalish IDE except Delphi (main reason why not to use Delphi anymore is its price). True, maybe I can use shared lib for point 2, haven't searched for this much.

Thank you for ideas/comments/experience.
Lazarus 1.7 with FPC 3.1.1.

http://www.tkweb.eu/

Phil

  • Hero Member
  • *****
  • Posts: 2324
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #1 on: October 15, 2016, 08:19:15 pm »
True, maybe I can use shared lib for point 2, haven't searched for this much.

A dynamic library is a good way to preserve the value of your investment in existing Pascal code.

You can use a dynamic library with iOS apps now if you put it in a framework (not difficult to do).

For iOS development you need an IDE with good integration with Xcode. Lazarus is not that tool, so Xamarin Studio or RemObjects' Fire IDE would be better choices.

Here are my notes on dynamic libraries:

https://macpgmr.github.io/MacXPlatform/PascalDynLibs.html


felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3537
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #2 on: October 15, 2016, 09:15:48 pm »
I have an app in the Google Play store using the "dynamic library" method, connecting Pascal to Java via JNI. There is some info on how to do this here: http://wiki.freepascal.org/Android_Programming

I think it is the best method. The Android User Interface will need to be in Java when using this method, but despite this minor problem, it works very well. The "brain" of the app is in Pascal, and I develop/debug this brain using a test Windows GUI built with Lazarus.

Leledumbo

  • Hero Member
  • *****
  • Posts: 7744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #3 on: October 15, 2016, 09:36:49 pm »
For now, go with what others said: dynamic library. We don't have yet a GUI library that abstracts both Android and iOS (and WP if that counts), let alone easy IDE setup.

tk

  • Sr. Member
  • ****
  • Posts: 354
    • tkweb
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #4 on: October 17, 2016, 12:55:32 pm »
Thank you all I will go this way.

The question is how to import the dyn lib and use it from Xamarin etc.. I've already studied the pascaldynlibs, thanks Phil. I've already tried the demos from swig (see this my post: http://forum.lazarus.freepascal.org/index.php/topic,34306.msg224434.html#msg224434).

To me, swig would be ok but the problem is that it does not work with Pascal but C++. First I thought it only takes a C++ header file and creates the wrappers in another languages but running the demo has shown to me that it also generates some C++ stuff, ie. the C++ class unwrappers. As if I could forget. They're needed in the dyn lib part, which in the swig case is always a C++ library.

But how to get this swig generated code back to Pascal, to build the dyn lib with FPC? Seems to be too much work already to me.

My idea is to make an own generator in Object Pascal (compilable in Delphi/FPC) based on a simple IDL (XML based, not like rather specific syntax of Microsoft or Borland IDLs for COM) that interfaces directly to Pascal (makes unwrappers/wrappers) and another languages (wrappers for C#,C++,...), maybe later also unwrappers for those languages. I would then make it available for all (maybe as part of my KControls).

My question is: Isn't it a duplicite work? Has already somebody sth. similar?

Thanks
« Last Edit: October 17, 2016, 02:43:07 pm by tk »
Lazarus 1.7 with FPC 3.1.1.

http://www.tkweb.eu/

Phil

  • Hero Member
  • *****
  • Posts: 2324
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #5 on: October 17, 2016, 04:57:27 pm »
Thank you all I will go this way.

Here are the steps I would use:

(1) Make sure your Pascal codebase actually compiles and runs correctly under iOS and Android, both 32-bit and 64-bit arm. These platforms supply libraries for HTTP. If you're not using them, then you need to make sure your desktop-tested communication code works on mobile too.

(2) Decide whether you really want to mirror exactly your Pascal classes in your wrapper classes. In my libraries, I did not want that at all. I wanted a more "controlled" subset of methods exposed via the wrapper classes.

(3) I'm not aware of any tool that does what you want to do. You may find that a lot more work than you think. It depends on how much you want to expose in your library. A handful of methods? Just do it by hand like I did in the example NDFD library. 500 methods? Well, maybe there it might make sense.

If you do create that kind of parser/generator, please let us know. I'm looking now to wrap the GDAL library functions in Pascal classes. It has hundreds of functions, so automating it could be very useful.

-Phil

tk

  • Sr. Member
  • ****
  • Posts: 354
    • tkweb
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #6 on: October 17, 2016, 06:17:15 pm »
Make sure your Pascal codebase actually compiles and runs correctly under iOS and Android, both 32-bit and 64-bit arm. These platforms supply libraries for HTTP. If you're not using them, then you need to make sure your desktop-tested communication code works on mobile too.

This I have to. Actually there are/will be several dyn libs. The central one where I want the generator for uses small scale plugins (other dyn libs written also in Lazarus) to do the actual UDP/HTTP communication. I/we hope the central dyn lib will work. If the UDP/HTTP plugin does not work on Android or iOS I have to create another plugins for them. Now I use Synapse for UDP/HTTP plugins and it works for me under all major desktop OSes (Win/Linux/OSX).

A handful of methods? Just do it by hand like I did in the example NDFD library. 500 methods? Well, maybe there it might make sense.

For starters I have 5 classes and about 120 methods (20 classic methods  + about 50 properties (=50 getters+50 setters)). But there will be more. I think the generator has sense.

Actually I've made the COM TLB in Delphi already, the (r)idl and COM altogether works very well for me. Shame such great thing it is not platform independent.

My idea is to take such ridl (for starters even more reduced ridl, no guids etc.) and basically do the same thing as Delphi TLB generator or Visual Studio Interop Assembly generator does.
It will only use the lowest-common ABI denominator, the C header file, as you said in my other post (and what swig also uses).
No parser needed (actually only a standard XML parser for my XMLish ridl format).
I could even imagine to build a simple GUI in Lazarus later that could manage the XML ridl file in a similar way as Delphi ridl designer does.
This will need some work but definitely it will be less work than to manage 100+ methods by hand now, and doing maintenance later also by hand.

Lazarus 1.7 with FPC 3.1.1.

http://www.tkweb.eu/

Phil

  • Hero Member
  • *****
  • Posts: 2324
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #7 on: October 17, 2016, 06:37:36 pm »
Now I use Synapse for UDP/HTTP plugins and it works for me under all major desktop OSes (Win/Linux/OSX).

If you need test code for various HTTP clients, the example NDFD library in part 3 can be compiled to use Synapse, Indy, FPC's HTTP client, or Apple Foundation-based code that uses the NSURLConnection class:

https://developer.apple.com/reference/foundation/nsurlconnection

Note that mobile generally means asynchronous.

It will only use the lowest-common ABI denominator, the C header file, as you said in my other post (and what swig also uses).

Sounds like you already have the library written and the C header file for it? If so, you're already half way there. Instead of just talking about it, I would just plunge in and start writing those wrapper classes. In general, creating those is the least of one's problems, as I predict you'll discover for yourself.


tk

  • Sr. Member
  • ****
  • Posts: 354
    • tkweb
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #8 on: October 17, 2016, 06:54:21 pm »
If you need test code for various HTTP clients, the example NDFD library in part 3 can be compiled to use Synapse, Indy, FPC's HTTP client, or Apple Foundation-based code that uses the NSURLConnection class:

Thanks, this will definitely help me if I will have problems with the HTTP plugin.

Sounds like you already have the library written and the C header file for it?

Yes I have the dll but now only as a COM server, compiled in Delphi XE.
The same codebase is compilable/runnable in Lazarus (under Win, Linux&OSX halfway tested), but I have no dll/so/dylib, it only works when directly linked to a Lazarus app.

EDIT: Finally the library should work for major desktop and mobile platforms, if that was not clear to you before.

EDIT2: And of course, I would like to discuss this before I spend weeks on doing something that might not be needed at all (although, in the case of the generator I've talked about, I don't see incredible amount of work before me now, should not be that hard...).
« Last Edit: October 17, 2016, 07:10:34 pm by tk »
Lazarus 1.7 with FPC 3.1.1.

http://www.tkweb.eu/

Phil

  • Hero Member
  • *****
  • Posts: 2324
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #9 on: October 17, 2016, 07:26:56 pm »
Yes I have the dll but now only as a COM server, compiled in Delphi XE.
The same codebase is compilable/runnable in Lazarus (under Win, Linux&OSX halfway tested), but I have no dll/so/dylib, it only works when directly linked to a Lazarus app.

EDIT: Finally the library should work for major desktop and mobile platforms, if that was not clear to you before.

Yes, that's good. You'll want to be able to test it in as many ways as you can. With one of my libraries, I also test the code linked in as well as part of a COM server that's registered when the .exe runs. It's important that the code work the same everywhere.

With COM, clients discover the server via the Registry. With a library, you either let the OS load it (if it can find it), or you manually locate and load it yourself. The NDFD example library only does the former, meaning that the library needs to be somewhere where the OS can find it. With Windows, that typically means in the same folder as the .exe; with Linux, that typically means putting it in /usr/lib; with OS X, that typically means putting it inside the .app bundle. Note that with iOS, you must put the library in a local framework within the bundle.

To load the library manually ("dynamically"), you can declare a library function like this to provide flexibility:

Code: [Select]
{$IFDEF LOAD_DYN}type TNdfdGetLibVersion = procedure{$ELSE}procedure NdfdGetLibVersion{$ENDIF}
  (VerBuf    : NdfdPChar;
   VerBufLen : NdfdUInt); {$IFDEF USE_CDECL}cdecl{$ELSE}stdcall{$ENDIF};
  {$IFNDEF LOAD_DYN}external LibNameBase;{$ENDIF}


In Pascal, to load the library, use LoadLibrary. Then determine if it has the functions you need using GetProcAddress. With other languages, you'll need to figure out how to do this. I omitted this option to keep my example library relatively simple.
« Last Edit: October 17, 2016, 07:28:31 pm by Phil »

tk

  • Sr. Member
  • ****
  • Posts: 354
    • tkweb
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #10 on: October 30, 2016, 07:03:07 pm »
Phil:
May I ask you if you still have some reason to use stdcall?
When I started with dyn libs long ago I used this calling convetion on Windows, then I went from Delphi to Lazarus, ported apps for Linux and OSX and there I had to use cdecl.
And now, when I test on Windows with cdecl I don't see any problem, at least with FPC/Laz.

FYI:
I already have written some portions of my generator, I called it PWIG (Pascal Wrapper and Interface Generator). You may check this out, is part of my Kcontrols now (download recent snapsot from Bitbucket and open the tools folder). I made simple demos for Object Pascal. My big library already uses this as well and seems to work well. The generator automates strings and even events (callbacks from the library). So it works pretty much like the Delphi COM tools and work with the wrapper classes is very comfortable. Now I have to add wrappers for C# to be able to communicate with the runtime from a C# app. Then, work will continue towards mobile platforms. Later I plan to add C++ wrappers as well, because my library should also be accessible from a C++ app.
Lazarus 1.7 with FPC 3.1.1.

http://www.tkweb.eu/

molly

  • Hero Member
  • *****
  • Posts: 2043
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #11 on: October 30, 2016, 07:09:40 pm »
And now, when I test on Windows with cdecl I don't see any problem, at least with FPC/Laz.
there are difference in cdecl vs stdcall where some things (sorry it escapes me from mind which things those are exactly. Perhaps someone else knows from mind) are automatically addresses with stdcall while not with cdecl. As long as you do not meet those cases, then  you should be good to go. I would not rely on it though.

tk

  • Sr. Member
  • ****
  • Posts: 354
    • tkweb
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #12 on: October 30, 2016, 09:19:07 pm »
Lazarus 1.7 with FPC 3.1.1.

http://www.tkweb.eu/

molly

  • Hero Member
  • *****
  • Posts: 2043
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #13 on: October 30, 2016, 11:32:32 pm »
well, this might be able to tell you some things as well.

But, i was specifically referring to FPC differences with regards to parameters automatically being converted or something to that extend. I've read a topic on this forum a while ago where marcov made some correction to someone using stdcall/cdecl which pointed me to the a page explaining what stdcall does to some parameter declarations, but for the life of me i am unable to find back that post as well as that page in the manual  :'(
« Last Edit: October 30, 2016, 11:40:38 pm by molly »

Phil

  • Hero Member
  • *****
  • Posts: 2324
Re: Mobile dev.: Lazarus or Xamarin?
« Reply #14 on: October 30, 2016, 11:40:22 pm »
May I ask you if you still have some reason to use stdcall?
When I started with dyn libs long ago I used this calling convetion on Windows, then I went from Delphi to Lazarus, ported apps for Linux and OSX and there I had to use cdecl.
And now, when I test on Windows with cdecl I don't see any problem, at least with FPC/Laz.

I tried to put everything I know about writing and using dynamic libraries in the article series and there's some discussion and hints there about why one would select stdcall vs. cdecl.

https://macpgmr.github.io/MacXPlatform/PascalDynLibs.html

Certainly, if you're writing a library to be used by an app on Windows that expects stdcall, then you wouldn't have any choice there.

One drawback with stdcall and a 32-bit lib is that apps compiled with MSVC will expect the lib function names to be decorated, although even that can be worked around by linking with GCC instead of the MS linker (use -Wl,--enable-stdcall-fixup switches).

With .NET, you can specify either StdCall or Cdecl calling convention, but MS also provides CallingConvention.Winapi, which means use StdCall on Windows and Cdecl on non-Windows. So that certainly seems like a hint about what MS thinks one should use.

Since it's so easy conditionally compile a lib function's calling convention with Pascal, that's what I always do with production code (Stdcall on Windows, Cdecl everywhere else). However, the NDFD library from Part 3 of the article series uses cdecl by default for all platforms and works fine across a range of languages on Windows (.NET, Python, C).

Note that the GIS GDAL library whose Pascal interface is included with Part 3's source files uses {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF} for some functions, but cdecl for others because that's the way the library does it, probably because of a combining of code over time from different authors/projects.

I've also worked with at least one library written with MSVC that uses cdecl for all of its functions.

 

Recent

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