P /从单声道在Ubuntu操作系统执行g ++ [英] P/Invoke g++ from mono in ubuntu OS

查看:147
本文介绍了P /从单声道在Ubuntu操作系统执行g ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能从单声道调用g ++的可执行文件的功能在Ubuntu?需要注意的是C ++和C#代码在Ubuntu操作系统编译

Is it possible to invoke a g++ executable file's function from mono in Ubuntu? Note that both C++ and C# code compiled in Ubuntu Operation System.

C ++应用程序源:

C++ application source:

#include <stdlib.h>

static int32_t Sum(int32_t a, int32_t b){
    return a + b;
}
/*
 * 
 */
int main(int argc, char** argv) {
    return (EXIT_SUCCESS);
}



C#单应用程序源:

C# mono application source:

using System;
using System.Runtime.InteropServices;

namespace MonoCsTest
{
 class MainClass
 {

  [DllImport("/home/.../MonoCsTest/bin/Debug/testcpp")]  
  public static extern Int32 Sum(Int32 a, Int32 b);

  public static void Main (string[] args)
  {
   Console.WriteLine (" 5 + 6 = " + Sum(5,6));
  }
 }
}

这将引发 DllNotFoundException

推荐答案

您需要编译库作为共享库:静态库可以't在以P / Invoke的运行时加载。
你添加一个main()函数的事实表明,你正在编译代码成可执行代替。
所以对于你的第一件事就是学习如何编译共享库,你可以尝试这样的:

You need to compile the library as a shared library: a static library can't be loaded at runtime with P/Invoke. The fact that you added a main() function suggests that you're compiling the code into an executable instead. So the first thing for you is to learn how to compile a shared library, you can try something like:

gcc -shared -o libtestcpp.so testcpp.cpp

然后,更改的DllImport名的路径完整的图书名称:

Then change the DllImport name to the path to the complete library name:

DllImport("/home/yourlogin/MonoCsTest/bin/Debug/libtestcpp.so")

您提出的其他错误是不考虑的C ++ manadated名称重整:这里的简单的解决方案是出口额()作为C函数与的externC周围{}

The other mistake you made is not considering the C++ manadated name mangling: the simpler solution here is to export Sum() as a C function surrounding it with extern "C" {}.

要诊断此类错误常常是有用

To diagnose such mistakes it is often useful to enable the debug logging from mono using:

MONO_LOG_LEVEL="debug" MONO_LOG_MASK="dll" mono yourprog.exe

这篇关于P /从单声道在Ubuntu操作系统执行g ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆