MonoTouch/Xamarin.iOS linker error with libTools

Q:

Basically I created Monotouch bindings for the PDFNet library. That part I finally got more or less working but I also have to get the tools library linked in and that is where I have hit a brick wall.
I have the latest versions of everything and I have made a custom build of the tools project, which now works fine when I compile it in a test project with Xcode. The problem has now gotten down to the following error from the linker:

/libTools.a(AnnotEditTool.o) contains undefined reference for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It doesn’t matter what architecture I build for – I get the exact same error for each one. I checked and the library seems to contain everything and as far as I can tell I am linking in all the required framework dependencies so I am at a total trying to figure what is missing. Any ideas? Looking online it seems I am not the only one who has been experiencing this problem and the error message is completely unhelpful here so I am hoping you might has seen this before and can offer a solution.

A:

I’ve never seen this error occur for the tools library, only when trying to use libPDFNet.a. The three things that have solved it for PDFNet.a are:

  • Using “Apple LLVM Compiler” and not “LLVM GCC 4.2” (which it appears you are already doing)

  • Not using the linker flag -all_load but instead adding -force_load /path/to/libAStaticLibrary.to libraries that need it

  • Using lstdc++ and not libc++, done by adding -lstdc++ to the other linker flags (older versions of XCode), or by selecting libstdc++ from the C++ Standard Library dropdown (newer versions of XCode).

One of our customers ran into similar issue, and this is what worked for them.

Have you linked the libstdc++? Depending on which version of XCode you’re using do this in the build settings either by adding -libstc++ to the other linker flags, or by selecting libstdc++ from the C++ Standard Library dropdown. Please consult the readme for other important information to know when starting a project from scratch.

And another customer said this worked for them with a similar error.

I found the problem, in our project we use more libraries and we used the other linker flags : -all_load -ObjC -lstdc++

when I remove the -all_load and -ObjC the project compiles.

I’ve never seen this error occur for the tools library, only when trying to use libPDFNet.a. The three things that have solved it for PDFNet.a are:

  • Using “Apple LLVM Compiler” and not “LLVM GCC 4.2” (which it appears you are already doing)
  • Not using the linker flag -all_load but instead adding -force_load /path/to/libAStaticLibrary.to libraries that need it
  • Using lstdc++ and not libc++, done by adding -lstdc++ to the other linker flags (older versions of XCode), or by selecting libstdc++ from the C++ Standard Library dropdown (newer versions of XCode).

A couple of questions:

  1. Does this error only occur when using Xamarin.iOS, i.e. are you able to use your tools library with the included XCode sample project?
  2. Can you compile a Xamarin.iOS project that uses PDFNet, with only libTools.a causing the linker problem?