Page 1 of 1


PostPosted: Sat Jun 30, 2012 11:38 pm
by Charles
One of our users has taken an interest in writing iPhone apps in Cobra. This is currently possible with C# and .NET through the Xamarin MonoTouch product. Given that Cobra runs on .NET and even generates C# for its back-end, this should be possible.

But this is new territory for Cobra so it's going to start out rough and we'll make it smoother as we go along. This will be our thread for discussion and issues on Cobra+MonoTouch+iPhone.

MonoTouch can work with .NET assemblies, aka DLLs, but only if they are "compiled against the MonoTouch profile". This means referencing the mscorlib.dll and System.dll of MonoTouch rather than the usual ones for .NET/Mono 4.0. Those are the most fundamental libraries in the CLR universe.

My first step in this process was finding how to do this with C# at the command line. I found the info here: ... le-from-th

I then had to enhance Cobra to be able to target a different CLR profile, so I added a new -clr-profile command line option in changeset:2728 which you'll see me use below.

First some basic steps to get the requisite software in place:

-- I'm on Mac OS X 10.6.8

-- Installed latest Mono available today:

My "mono --version" went from:
Mono JIT compiler version 2.10.9 (tarball Tue Mar 20 15:31:37 EDT 2012)
Mono JIT compiler version 2.10.9 (tarball Mon May 7 20:25:51 EDT 2012)

Same version, but newer build date. Makes you wonder.

-- Installed latest MonoDevelop which was I did a fresh install rather than using their updater which I had problems with.

-- Installed the current MonoTouch eval obtained from the Xamarin web site (monotouch-eval.dmg, 7.8MB, version 5.2.12).

-- Then a baby test for MonoTouch:
---- MonoDevelop > File > New Solution > C# > MonoTouch > "MonoTouch Library Project"
------ Build succeeded.
------ Added a C# file with some dummy code. Build succeeded.

-- Check that the MonoTouch C# compiler exists from the command line:
Code: Select all
$ /Developer/MonoTouch/usr/bin/smcs
error CS2008: No files to compile were specified

-- Tested "regular" compilation of a C# library:

---- Create Foo.cs:
Code: Select all
using System;

namespace Foo {

   public class Bar {

      public Bar() : base() { }



---- At the command line:
Code: Select all
$ dmcs /t:library Foo.cs
$ ls -l Foo.dl
$ rm Foo.dll

-- Test a MonoTouch compilation of that Foo.cs:
Code: Select all
$ /Developer/MonoTouch/usr/bin/smcs /noconfig "/out:Foo.dll" "/r:/Developer/MonoTouch/usr/lib/mono/2.1/System.dll" "/r:/Developer/MonoTouch/usr/lib/mono/2.1/System.Xml.dll" "/r:/Developer/MonoTouch/usr/lib/mono/2.1/System.Core.dll" "/r:/Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll" /nologo /warn:4 /debug:+ /debug:full /optimize- /codepage:utf8 "/define:DEBUG" /t:library "Foo.cs"
$ ls -l Foo.dl
$ rm Foo.dll

-- Now try with Cobra:

-- Create Foo.cobra:
namespace Foo

class Bar

cue init

-- If you don't have the latest Cobra, see HowToInstallFromSource. Be sure to install for .NET 4 when prompted.

-- At the command line:
Code: Select all
$ cobra -about
The Cobra Programming Language svn:2727 (post 0.8) / 2012-06-30
on Mono 2.10.9 CLR v4.0.30319 on Mac OS X 10.6.8
at /usr/local/cobra/Cobra-svn-2727/bin/cobra.exe

-- And now the moment you have been waiting for:
Code: Select all
cobra -c -t:lib -out:Foo.dll \
-v:1 \
-embed-run-time:yes \
-clr-profile:/Developer/MonoTouch/usr/lib/mono/2.1 \
-native-compiler:/Developer/MonoTouch/usr/bin/smcs \
-native-compiler-args:/noconfig \
-native-compiler-args:/codepage:utf8 \
-native-compiler-args:/warn:4 \
-native-compiler-args:/debug:+ \
-native-compiler-args:/debug:full \
-native-compiler-args:/optimize- \
-native-compiler-args:/codepage:utf8 \
-native-compiler-args:/define:DEBUG \
-ref:/Developer/MonoTouch/usr/lib/mono/2.1/System.dll \
-ref:/Developer/MonoTouch/usr/lib/mono/2.1/System.Xml.dll \
-ref:/Developer/MonoTouch/usr/lib/mono/2.1/System.Core.dll \
-ref:/Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll \

I would like to put all those -native-compiler-args together, but Mono's own Environment.getCommandLineArgs splits them up even if I quote them.

In any case, once you set up your project, you'll almost certainly put this in a bash script or something similar and not worry about it. In fact, that's what I did.

You'll notice the new -clr-profile option in there.

You can take out the -v:1 if you want less output.

That's as far as I'm taking things for now. I'm on the hook for the core compiler work and other things. Those of you interested in the iPhone can read that stackoverflow article which contains additional info. And/or you can experiment with referencing the Cobra-produced DLL from a MonoTouch project created in MonoDevelop.

Ultimately, I hope others chime in with tips and solutions and carry this further to create a real example. When things are refined and stabilize, an informative wiki page would be nice. I'll tweak and enhance the compiler as needed to eliminate roadblocks.

Re: iPhone

PostPosted: Sat Jun 30, 2012 11:46 pm
by Charles
I forgot to mention that this Cobra compiler option:
Code: Select all

Can lead to this error:
Code: Select all
Unhandled Exception: System.UnauthorizedAccessException: Access to the path "/usr/local/cobra/Cobra-svn-2727/bin/Cobra.Lang/CobraCore.cobra.cs" is denied.

Cobra's -embed-run-time:yes generates C# into the same directory as the standard library files. Needs to be fixed. Quick hack:
Code: Select all
$ sudo chown -R USERNAME /usr/local/cobra/Cobra-svn-VERSION/

Re: iPhone

PostPosted: Wed Sep 26, 2012 12:50 pm
by torial
Do you know if anyone has succeeded in developing an iPhone app using Cobra on top of MonoTouch?

Re: iPhone

PostPosted: Wed Sep 26, 2012 2:01 pm
by DelphiGuy
Although I'm famous in Charles' previous post for being the guy planning to do exactly that, I haven't even scratched the surface yet. I believe that no one has tried previously, other than Charles' good-natured first attempt as described previously.

Re: iPhone

PostPosted: Wed Sep 26, 2012 3:23 pm
by Charles
Correct, no one has taken this all the way. When I thought DelphiGuy was eager to jump on it, I cranked about the above work which I felt was the hardest first step and the one I could best do given my knowledge of Cobra and Mono.

Re: iPhone

PostPosted: Wed Sep 26, 2012 8:46 pm
by DelphiGuy
Hey, I _was_ eager to jump on it but I quickly realized I had a heckuva lot more to learn before I could take advantage of your first foray into Monotouch. I'll get there. I'm working on it most every day.

Re: iPhone

PostPosted: Thu Sep 27, 2012 7:49 am
by torial
I may get a chance to work in this environment -- if so, I'll see if I can get it working, I'm not the decision maker on this, but the guy who is uses VB.Net and since that isn't supported for MonoTouch, he may be open to Cobra.

Re: iPhone

PostPosted: Thu Sep 27, 2012 10:15 am
by Charles
As you run into problems--and since you would be "going first" you will--let me know and we'll see if we can knock them down. I have a Mac, Xcode, MonoDevelop, etc.