Using the C / C ++ library in a Visual Studio Mac solution (Xamarin.Mac application)?

Xamarin.Mac And C ++

I'm trying to use the C ++ library (.a) in Visual Studio for Mac (aka Xamarin) I found some information on the Internet about linking native libraries in Xamarin ( https://developer.xamarin.com/guides/ios/advanced_topics/native_interop / ), but it is deprecated since Visual Studio for Mac is missing.

I downloaded both projects at https://github.com/dawmster/XamarinAndCpp

Content

  • MyCppLib Project - Xcode creating libMyCppLib.a
  • XamarinMacApp - Solution for Visual Studio for Mac
  • libMyCppLib.a is a MyCppLib product, XamarinMacApp refers to it.

What's alright

MyCppLib compiles fine and creates libMyCppLib.a I intended to export only two C functions ( my_C_Function, my_second_C_Function- MyCppLib.cpp ), but I cannot remove other sibols that seem to be the root of the problem

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

XamarinMacApp refers to libMyCppLib.a (added to the 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 do not like

XamarinMacApp - does not compile with the following error:

Please note that the functions are C ( my_C_Function, my_second_C_Functionnot listed below). It seems that only the standard C ++ library cannot be linked.

Complete Visual Studio Mac Compiler .

Full Xcode Compiler Protocol

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 on how to move forward?

+6
source share

All Articles