paradox
paradox

Reputation: 31

Binding a static library into a Xamarin iOS project

I've decided to take a step back and just try to get a very simple proof of concept to work.

I've progressed a little further thanks to one of the Xamarin Lightning Lectures around this topic. I’ve been following this tutorial: https://university.xamarin.com/lightninglectures/ios-bindings-in-cc

Here’s what I’ve done so far:

1) I have a simple C file “example.c” with 3 functions: addOne(int n), subtractOne(int n), get_time()

2) As shown in the tutorial, I used SWIG to create a C wrapper class - “example_wrap.c” (along with the C# wrapper classes).

3) I created the static library in Xcode and built a FAT binary with all architectures (lipo used to combine libs).

The tutorial talks about creating 2 projects - one with the C# wrapper classes and the other a single view iOS app. The iOS project has the libexample.a static library(as a BundledResource). Everything was going well until trying to build the iOS app. I can't get the iOS app to compile. I've removed any calls to the static library for now to isolate the issue....I'm just trying to get it to compile and link correctly. So all I have now is just a blank iOS app with the bundled static library. But I'm still having problems.

I have the following added to the mtouch arguments: -cxx -gcc_flags "-L${ProjectDir} -lexample -force_load ${ProjectDir}/libexample.a"

When compiling, I get these errors:

Undefined symbols for architecture x86_64:
"subtractOne(int)", referenced from:
_CSharp_subtractOne in libexample.a(example_wrap.o)
"addOne(int)", referenced from:
_CSharp_addOne in libexample.a(example_wrap.o)
"get_time()", referenced from:
_CSharp_get_time in libexample.a(example_wrap.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I double checked the symbols in the libexample.a file by running “nm -arch x86_64 libexample.a” from the command line. It appears that all the symbols are listed for that architecture.

Here’s a link to the zip file with my libexample.a and C files: https://drive.google.com/open?id=0B2WtJ38rvldKYmlZckU3QjNBeUE

I’m sure this is just a configuration error but I can’t seem to pinpoint where I’m going wrong.

Thanks.

Upvotes: 1

Views: 1163

Answers (1)

paradox
paradox

Reputation: 31

I thought I'd answer my own question here - after getting some help from Xamarin support, it looks like I might have mixed up how I created the static library - I had to make sure the files were compiled as 'C' files, instead of C++. After I generated the static library correctly, the compiler was able to recognize the missing symbols.

Upvotes: 1

Related Questions