[MonoTouch] linking dependency

jhandel at gmail.com jhandel at gmail.com
Tue Mar 13 18:53:34 UTC 2012


Thanks thats what I needed!

Josh
________________________________
From: Jeff Stedfast [jeff at xamarin.com]
Sent: Tuesday, March 13, 2012 1:42 PM
To: jhandel at gmail.com
Cc: monotouch at lists.ximian.com
Subject: Re: [MonoTouch] linking dependency

Hi Josh,

If you are writing a binding to a native library, you probably want to take advantage of LinkWithAttribute(s) instead of specifying gcc flags in MonoDevelop.

The LinkWithAttribute is an assembly level attribute that you probably want to either put in your AssemblyInfo.cs, or, if you are using monoDevelop's "MonoTouch Binding Library" project template, in the code-behind file named <libMyLibrary>.linkwith.cs that gets auto-generated when you add libMyLibrary.a to your project[1].

This attribute will look something like this:

[assembly: LinkWith ("libMyLibrary.a", LinkTarget.Simulator | LinkTarget.ArmV6 | LinkTarget.ArmV7, ForceLoad = true)]

(MonoDevelop smartly auto-detects which architectures the .a is compatible with, so it's probably safe to leave that field alone)

There are a few other properties for LinkWithAttributes that you can set:

* LinkerFlags: This is where you can list other linker flags you'd like to pass, e.g. in your cause you'd set it to: "-lz -lsqlite3"

* Frameworks: This allows you to specify a list of frameworks that the native library depends on, e.g.: "Foundation UIKit"

(in your case, you can leave it empty because both Foundation and UIKit will be pulled in by the main application anyway, but it doesn't hurt to add them)

* WeakFrameworks: Same idea as Frameworks, but for weak framework references.

* IsCxx: This is a boolean property that specifies whether or not the native library requires the use of the c++ linker. Defaults to false.

* NeedsGccExceptionHandling: Another boolean property which is sometimes needed by c++ libraries (set to true if the native library requires linking with -lgcc_eh).

* ForceLoad: This is a boolean hint for the compiler to use -force_load on the native library (this usually needs to be set to true).


So, for your particular example, you'd probably want a LinkWith similar to the following:

[assembly: LinkWith ("libMyLibrary.a", LinkTarget.Simulator | LinkTarget.ArmV6 | LinkTarget.ArmV7, LinkerFlags = "-lz -lsqlite3", ForceLoad = true)]


Hope that helps answer your question,

Jeff


1. Note: There's a bug in MonoDevelop 2.8.6.* that will cause MonoDevelop to re-generate the *.linkwith.cs files every time you open the project (sorry about that, it's my fault). Hopefully 2.8.8 stable will be going out later this week or next week.


On Tue, Mar 13, 2012 at 1:32 PM, Josh Handel <jhandel at gmail.com<mailto:jhandel at gmail.com>> wrote:
Howdy all,
  I am trying to wrap Localystics API and its dependent on a few
libraries (like sqlite).. I have read thru the binding documentation
but I am still confused where in MonoDevelop to put my extra GCC flags
to link libraries.. I need to link the following libraries (I think,
maybe more).

* libz.dylib
* UIKit.framework
* libsqlite3.dylib

It also uses Foundation.framework but I don't think thats an issue :-P

Thanks
Josh Handel
PS: Thanks for making a Binding Library project.. Last time I did
binding I had to do it all from commandline (ick).
_______________________________________________
MonoTouch mailing list
MonoTouch at lists.ximian.com<mailto:MonoTouch at lists.ximian.com>
http://lists.ximian.com/mailman/listinfo/monotouch

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/monotouch/attachments/20120313/83d0a095/attachment-0001.html>


More information about the MonoTouch mailing list