c# - monodevelop System.DllNotFoundException 调用 C++ 库中的函数

标签 c# c++ linux dll monodevelop

我有一个为 Linux Ubuntu 32 位编译的 C++ 库 libmylib32.so。

该库位于 usr/local/lib 中,我确认它正在 C++ 程序中工作。

在文件/etc/mono/config 中我添加了

<dllmap dll="mylib32.dll" target="/usr/local/lib/libmylib32.so"/>

我也尝试过

<dllmap dll="mylib32.dll" target="/usr/local/lib/libmylib32.so" os="!windows"/>

我用monodevelop编写了一个C#程序来调用C++函数:

short libhndl( const char *, unsigned short, long, unsigned short * );

首先我编写了一个类,其中声明了函数的接口(interface):

public class MyClass
{

[global::System.Runtime.InteropServices.DllImport("mylib32.dll", EntryPoint="libhndl", CharSet = CharSet.Ansi)]
public static extern short libhndl( [MarshalAs(UnmanagedType.LPStr)] string ip, ushort port,int timeout, out ushort libHndl);

}

主程序调用函数libhndl:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;


public class main_program
{
// ...
 ret = MyClass.libhndl(ip_address, tcp_port, tcp_timeout, out m_libHndl);
//...
}

如果我调试程序,当调用函数时我会得到异常:

System.DllNotFoundException: /usr/local/lib/libmylib32.so
 at (wrapper managed-to-native) MyClass:libhndl (string,uint16,int,uint16&)
 at main_program.function () [0x00056] in
 /home/f90100027/workspace/Ex_mono/Ex_mono/main_program.cs:306 
 at System.Threading.Thread.StartInternal () [0x00000] in <filename unknown>:0

该库似乎找到了该库,因为异常显示了完整的库路径。

谁能解释一下问题出在哪里吗?

预先感谢您的帮助。

按照建议设置环境变量 MONO_LOG_LEVEL=debug 调试器返回

Mono: DllImport error loading library 'libmylib32.so': '/usr/local/lib/libmylib32.so: undefined symbol: _ZTVN10__cxxabiv117__class_type_infoE'. 

命令 nm/usr/local/lib/libmylib32.so 返回

...
000144c0 T libhndl
...
0013ee30 V _ZTV9SocketMgr 
         U _ZTVN10__cxxabiv117__class_type_infoE
...

最佳答案

我不完全确定 mono 如何与 C++ 代码一起工作,但我想你的问题与 C++ 名称修饰有关。尝试将代码包装在 extern C block 中。

尝试

extern "C" {
short libhndl( const char *, unsigned short, long, unsigned short * );
}

相反。

关于c# - monodevelop System.DllNotFoundException 调用 C++ 库中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36478188/

相关文章:

C++ 温度转换

linux - 执行时出错-找不到所需的版本 `Qt_5'

linux - 如何使用 ssh 通过提供静态文本来执行与键盘交互的操作

c# - 如何在 ASP.NET 中实现 Windows 服务类型的基础结构

c# - 托管到服务器时 WCF 出错

C# 手动填充 double[][]

c# - 删除 XYZ 点列表中的重复项

c++ - Stringstream 的 Str 方法将不起作用。 (不同类型的串联)(C++)

c++ - Sieve of Eratosthenes 算法的效率

php - 以不同(非 root)用户身份从 PHP 运行 shell 脚本