Bug 16015 - DllNotFoundException when consuming native C library
Summary: DllNotFoundException when consuming native C library
Status: RESOLVED INVALID
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 7.0.2.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-11-07 15:12 UTC by Jon Goldberger [MSFT]
Modified: 2013-11-08 14:07 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
App project that consumes the native C static library (15.06 KB, application/zip)
2013-11-07 15:13 UTC, Jon Goldberger [MSFT]
Details
Static C library Xcode project (22.67 KB, application/x-7z-compressed)
2013-11-07 15:14 UTC, Jon Goldberger [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Comment 1 Jon Goldberger [MSFT] 2013-11-07 15:13:59 UTC
Created attachment 5361 [details]
App project that consumes the native C static library
Comment 2 Jon Goldberger [MSFT] 2013-11-07 15:14:30 UTC
Created attachment 5362 [details]
Static C library Xcode project
Comment 3 Jon Goldberger [MSFT] 2013-11-07 15:15:42 UTC
In the process of trying to learn how to link native libraries in Xamarin.iOS, I'm trying to link a very simple static C library in a Xamarin.iOS project. Xamarin however seems to be unable to find the library whenever I try to access a function from the library. The library consists of only one source and header file, with one function (Hello world), and is made in Xcode (as a C library project). I've followed you're guide on linking native libraries (http://docs.xamarin.com/guides/ios/advanced_topics/native_interop). I have built the static library using the terminal, as you suggest in the guide, and I get the *.a file as expected. After importing the .a file to a Xamarin.iOS project, and linking it, the problem appears when I try to access my Hello world function from the library using P/invoke. When debugging, I get a DllNotFoundException when the function from the library is called. I have run out of ideas on how to solve this, and hope you can help me.
This is the first real problem I've encountered using Xamarin, and I really want to congratulate you guys on making a very powerful cross platform development tool. It's been a joy working with both Xamarin.iOS and Xamarin.Android!
Comment 4 Sebastien Pouliot 2013-11-07 17:31:47 UTC
There a few things wrong here. First:

		[DllImport("libHello_World.a",EntryPoint="Hello_World")]

^ that's bad - because a ".a" is a static library and once natively linked it will be part of the main executable (the ".a" is not shipped in the app). 

In order to look for the native method inside the main executable you need to use the special "__Internal" name, e.g.

		[DllImport("__Internal",EntryPoint="Hello_World")]

This is documented in the above link [1] in the "Static Libraries" section.


Next you're not giving any instruction to to link to this static library. As such it is not linked to the application and it the `Hello_World` function won't be part of the native executable (and won't be found).

This is also part of [1], see "Configure Xamarin.iOS To Link the Library"


[1] http://docs.xamarin.com/guides/ios/advanced_topics/native_interop