我用几个P / .NET下调用。不过,我想我的图书馆在Windows和Linux,preferably使用相同的二进制文件两种。
I am using several P/Invokes under .NET. However, I want my library to work both in Windows and Linux, preferably with the same binaries.
由于我依赖本机库可在多种平台上,我希望只让他们与我的托管库的二进制文件。
Since the native library I depend on is available on multiple platforms, I was hoping to just have them along with my managed library's binaries.
现在,我使用的东西是这样的:
Right now I'm using something like this:
[DllImport("/usr/lib/libMYLIBNAME.so.1")]
不过,这显然只适用于Linux操作系统。我正在考虑我可能复制二进制文件从/ usr / lib和分发与我的应用程序,这样我就可以减少以上为:
But this obviously only works for Linux. I was considering that I could possibly copy that binary from /usr/lib and distribute along with my application, so I could reduce the above to:
[DllImport("libMYLIBNAME.so")]
但是,这仍然是Linux的唯一。
But this still is Linux-only.
反正是有改变的库名字符串,因此它会寻找libMYLIBNAME.so在Linux和MYLIBNAME.dll在Windows上,或者非常类似的东西?
Is there anyway to change the library name string so it'd look for libMYLIBNAME.so under Linux and MYLIBNAME.dll on Windows, or something very similar?
我想避免任何需要重新编译为每个支持的平台......
I would like to avoid anything that requires recompilation for each supported platform...
(注:更妙的是,会找MYLIBNAME.dll在Windows和/usr/lib/libMYLIBNAME.so.1在Linux上的解决方案,但这种改善是可选的)
(Note: even better would be a solution that'd look for MYLIBNAME.dll on Windows and /usr/lib/libMYLIBNAME.so.1 on Linux, but this improvement is optional)
两件事情
1的DllImport不带扩展名 这是支持Windows,Linux和Mac,并导入相应的库为目标平台。
1- DllImport without the extension This is supported on Windows, Linux and MAC and will import the appropriate library for the target platform.
[DllImport("libMYLIBNAME")] -
2- preffered选项是使用< dllmap />
,它允许你映射导入库的名字到目标平台库名。所以,如果在Windows中有一个名为DLL mylib.dll
和相应的Linux所以 mylinuxlib.so.3.6.1
您可以使用Windows DLL名称导入此
2- The preffered option is to use the <dllmap/>
which allows you to map an import library name to the target platform library name. So if on Windows you have a dll called mylib.dll
and the corresponding Linux so is mylinuxlib.so.3.6.1
you can import this using the windows DLL name
[DllImport("mylib.dll")]
和配置添加到config这个名称映射到Linux库名称
And add a configuration to the config to map this name to the Linux library name
<configuration>
<dllmap dll="mylib.dll" target="mylinuxlib.so.3.6.1" />
</configuration>
了解更多这里