6

Xamarin.Mac And C++

I am trying using C++ library (.a) in Visual Studio for Mac (a.k.a. Xamarin) I've found online some info on linking native libraries in Xamarin (https://developer.xamarin.com/guides/ios/advanced_topics/native_interop/) but it seems outdated since Visual Studio for Mac is out.

I uploaded both project to https://github.com/dawmster/XamarinAndCpp

Contents

  1. MyCppLib - XCode project producing libMyCppLib.a
  2. XamarinMacApp - Visual Studio For Mac solution
  3. libMyCppLib.a - that is a product of MyCppLib, XamarinMacApp references it.

What is ok

MyCppLib compiles fine and produces libMyCppLib.a I intended to export only two C functions (my_C_Function, my_second_C_Function - MyCppLib.cpp) but I am unable to strip other sybols - which seems to be root of the problem

extern "C" {
        int my_C_Function();
        int my_second_C_Function();
}

In XamarinMacApp libMyCppLib.a is referenced (added to solution). And my_C_Function is called in AppDelegate.cs as follows:

using System.Runtime.InteropServices;

namespace XamarinMacApp
{

    [Register("AppDelegate")]
    public class AppDelegate : NSApplicationDelegate {

        public AppDelegate(){}

        [DllImport("__Internal")]
        static extern int my_C_Function();
        public override void DidFinishLaunching(NSNotification notification)
        {
            int myretval = my_C_Function();
        }

        public override void WillTerminate(NSNotification notification)
        {
            // Insert code here to tear down your application
        }
    }
}

What's not ok

XamarinMacApp - does not compile with following error :

Notice that C functions ( my_C_Function, my_second_C_Function is not listed in error below). It seems only C++ Standard Library cannot be linked.

Full Visual Studio Mac compiler log.

Full Xcode compiler log

Undefined symbols for architecture x86_64:
      "std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
          l004 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
          l005 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::ios_base::getloc() const", referenced from:
          l004 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
          l005 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(unsigned long, char)", referenced from:
          l007 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
          l007 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::basic_ostream<char, std::__1::char_traits<char> >::put(char)", referenced from:
          l004 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush()", referenced from:
          l004 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)", referenced from:
          l005 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()", referenced from:
          l005 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::cout", referenced from:
          l002 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::ctype<char>::id", referenced from:
          l004 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
          l005 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::locale::~locale()", referenced from:
          l004 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
          l005 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::ios_base::__set_badbit_and_consider_rethrow()", referenced from:
          l005 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::__1::ios_base::clear(unsigned int)", referenced from:
          l005 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "std::terminate()", referenced from:
          l008 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "operator delete(void*)", referenced from:
          l001 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "operator new(unsigned long)", referenced from:
          l001 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "___cxa_begin_catch", referenced from:
          l005 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
          l008 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "___cxa_end_catch", referenced from:
          l005 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
      "___gxx_personality_v0", referenced from:
          l004 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
          l005 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
          l007 in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
          Dwarf Exception Unwind Info (__eh_frame) in libMyCppLib.a(libMyCppLib.a-x86_64-master.o)
    ld: symbol(s) not found for architecture x86_64
    clang : error : linker command failed with exit code 1 (use -v to see invocation)

    MMP : error MM5109: Native linking failed with error code 1.  Check build log for details.
Done building target "_CompileToNative" in project "XamarinMacApp.csproj" -- FAILED.

Done building project "XamarinMacApp.csproj" -- FAILED.

Any ideas how to move forward?

Nick
  • 1,248
  • 12
  • 18
  • Are you linking using the c compiler, not c++? – πάντα ῥεῖ May 24 '17 at 11:42
  • Well, that is a good question. Xcode project is a C++ project (with STL). Visual Studio for Mac compiles with clang. I hunted following command from VSMac output: xcrun -sdk macosx clang -g -mmacosx-version-min=10.12 -arch x86_64 -fobjc-runtime=macosx -ObjC /Users/mikolaj/Programowanie/Tries/XamarinAndCpp/XamarinMacApp/XamarinMacApp/bin/Debug/XamarinWithCpp.app/Contents/MonoBundle/libMyCppLib.a -u _xamarin_nfloa etc........ and map: /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/bin/mmp /verbose /debug – Nick May 24 '17 at 11:43
  • The errors are typical for that. Can you show us the linker command line? – πάντα ῥεῖ May 24 '17 at 11:45
  • 1
    It should call `clang++`, I can't help you how to control that in the IDE though. – πάντα ῥεῖ May 24 '17 at 11:54
  • I put Xcode compiler log in gist: https://gist.github.com/dawmster/f848c18228a4fd0c3ce9c51b6db3fe8c#file-compiler_output-txt – Nick May 24 '17 at 11:55
  • And here is Visual Studio Mac compiler output: https://gist.github.com/dawmster/c6e1fabf3df3ec953faeacfbdce80af3#file-vsmac_xamarinmacapp_compileroutpu-txt – Nick May 24 '17 at 11:58
  • are you shure you aren't using different implementation of standart cpp library? – tty6 May 31 '17 at 08:33
  • Did someone found a solution for this? I'm currently trying the same. – Martin Jun 07 '19 at 18:50
  • I just dropped Xamarin and switched to XCode and objective-c/swift – Nick Jun 10 '19 at 09:01

0 Answers0