与Linux中的本机库互操作 [英] Interop with native libraries in linux
问题描述
我正在使用跨平台的C#编写应用程序.它对几个不同的库使用DllImport
.它也是x32和x64.
对于我自己编译的.dll/.so,它似乎正常工作.我将x32共享库放在x32子文件夹中,并将x64共享库放在x64子文件夹中.
I am writing an application in C#, which is cross platform. It uses DllImport
for several different libraries. It is also both x32 and x64.
It seems to work right for my own compiled .dll/.so. I put the x32 shared libraries in the x32 sub-folder, and the x64 shared libraries in the x64 sub-folder.
在通过.net框架的Windows上,我根据当前情况调用SetDllDirectory("x32" or "x64")
运行.在Mono上,通过Windows,Linux或Mac,我在RandomLibrary.dll.config中使用DllMap
.它适用于x32或x64目录中的文件.
On windows via .net framework I call SetDllDirectory("x32" or "x64")
depending on the current
runtime. On mono, either through windows, linux or mac, I use a DllMap
, in RandomLibrary.dll.config. It works fine for files that are located in the x32 or x64 directories.
但是,对于未写的库,例如FreeType2,我也使用DllImport
.在Windows上,我只是将构建的库推到两个子文件夹中.但是在Linux上,我宁愿使用libfreetype6库,您可以通过友好的本地程序包管理器安装该库.我可以将DllMap链接硬编码为/usr/lib/x86_64-linux-gnu/libfreetype
.so.6,但我认为freetype文件将根据您使用的发行版而改变.
However, I also use DllImport
for libraries that I didn't write, such as FreeType2. On windows, I just shove the built libraries in the two sub-folders. However on linux I would rather use the libfreetype6 library that you can install via your friendly local package manager. I can hardcode the DllMap link to /usr/lib/x86_64-linux-gnu/libfreetype
.so.6, but I assume that the freetype file will change depending on what distro you use.
有什么简便的方法可以找到libfreetype.so,而不管我使用的是哪个发行版?
Is there any easy way where it can just find libfreetype.so regardless of what distro I am using?
推荐答案
Mono为此专门设计了一个东西/程序,称为 DllMap .您可以指定在哪个平台上加载哪个非托管libaray.例如:
Mono have a thing/procedure specifically for this, it is called DllMap. You can specify which unmanaged libaray gets loaded on which platform. Eg:
<configuration>
<dllmap dll="foobar" os="linux" wordsize="64" target="linux/amd64/libfoobar.so" />
<dllmap dll="foobar" os="linux" wordsize="32" target="linux/i386/libfoobar.so" />
</configuration>
我自己还没有尝试过上述操作-dllmap文档似乎暗示目标值不是路径,但我不确定.
I've not tried the above myself - The dllmap docs seem to hint that the target value isn't a path but I can't be sure.
另一种方法是拥有一个简单的shellscript或批处理文件.在Linux上,shell脚本可以通过调用arch
并相应地设置LD_LIBRARY_PATH
来轻松确定体系结构.通常,我更喜欢在单声道程序周围使用外壳包装程序(在控制台上看到MyProgram.exe
很难看)
Another approach would be to have a simple shellscript or batch file. On linux the shell script could easily work out the architecture by calling arch
and set LD_LIBRARY_PATH
accordingly. I generally prefer a shell wrapper around my mono programs ( seeing MyProgram.exe
on the console feels ugly )
最后,如果您拨入 dlopen 或 LoadLibarary 来预加载您的共享库.
Lastly you might have more fun if you pinvoke into dlopen or LoadLibarary to pre-load your shared libraries.
托管应用启动时,您要确定自己的体系结构,然后在Linux上使用dlopen()
,在Windows上使用LoadLibaray()
.
When your managed app starts you decide what architecture you are and then use dlopen()
if on linux or LoadLibaray()
if on windows.
这篇关于与Linux中的本机库互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!