6

Xamarin.Mac 和 C++

我正在尝试在 Visual Studio for Mac(又名 Xamarin)中使用 C++ 库(.a)我在网上找到了一些关于在 Xamarin 中链接本机库的信息(https://developer.xamarin.com/guides/ios/advanced_topics/native_interop / ) 但它似乎已经过时,因为 Visual Studio for Mac 已经过时了。

我将两个项目都上传到了https://github.com/dawmster/XamarinAndCpp

内容

  1. MyCppLib - 生成 libMyCppLib.a 的 XCode 项目
  2. XamarinMacApp - Visual Studio For Mac 解决方案
  3. libMyCppLib.a - 这是 MyCppLib 的产品,XamarinMacApp 引用它。

什么可以

MyCppLib编译良好并生成 libMyCppLib.a 我打算只导出两个 C 函数(my_C_Function, my_second_C_Function- MyCppLib.cpp)但我无法剥离其他 sybols - 这似乎是问题的根源

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

在 XamarinMacApp 中引用了 libMyCppLib.a(添加到解决方案中)。在AppDelegate.cs中调用 my_C_Function如下:

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
        }
    }
}

什么不好

XamarinMacApp - 无法编译并出现以下错误:

请注意,C 函数(my_C_Function, my_second_C_Function未在下面错误中列出)。似乎只有 C++ 标准库无法链接。

完整的 Visual Studio Mac 编译器日志。

完整的 Xcode 编译器日志

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.

任何想法如何前进?

4

0 回答 0