我有一个为 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/