简体   繁体   English

.NET Core 中是否有等效于 DllImport 的 Linux?

[英]Is there a Linux equivalent of DllImport in .NET Core?

I am working on a .NET Core project.我正在处理一个 .NET Core 项目。 This should run on Linux without problems.这应该在 Linux 上运行没有问题。 However, I need to call a library that has been created with C++.但是,我需要调用一个用 C++ 创建的库。 Traditionally, C# developers do this with DllImport&DllExport .传统上,C# 开发人员使用DllImport&DllExport执行此 操作

However, I do not want to import or export dll's in a production environment.但是,我不想在生产环境中导入或导出 dll。 Over there it needs to run on Linux.在那里,它需要在 Linux 上运行。 Is there a Linux equivalent of DllImport/DllExport?是否有 Linux 等效的 DllImport/DllExport? On Linux, so files are used instead of dll files .在 Linux 上,因此使用文件而不是 dll 文件 I am not smart when it comes to low level things so I look for a quick solution and a clear example.当涉及到低级别的事情时,我并不聪明,所以我寻找一个快速的解决方案和一个明确的例子。 Something like an "SoImport"?像“SoImport”这样的东西?

You can use .so file also in DllImport.您也可以在 DllImport 中使用 .so 文件。

[DllImport ("libc.so")]
private static extern int getpid ();

From the dlopen(3) man page, the necessary shared libraries needed by the program are searched for in the following order:从 dlopen(3) 手册页,按以下顺序搜索程序所需的必要共享库:

  1. A colon-separated list of directories in the user's LD_LIBRARY_PATH environment variable.用户的LD_LIBRARY_PATH环境变量中以冒号分隔的目录列表。 This is a frequently-used way to allow native shared libraries to be found by a CLI program.这是允许 CLI 程序找到本机共享库的常用方法。
  2. The list of libraries cached in /etc/ld.so.cache. /etc/ld.so.cache 中缓存的库列表。 /etc/ld.so.cache is created by editing /etc/ld.so.conf and running ldconfig(8). /etc/ld.so.cache 是通过编辑 /etc/ld.so.conf 并运行 ldconfig(8) 创建的。 Editing /etc/ld.so.conf is the preferred way to search additional directories, as opposed to using LD_LIBRARY_PATH, as this is more secure (it's more difficult to get a trojan library into /etc/ld.so.cache than it is to insert it into LD_LIBRARY_PATH).编辑 /etc/ld.so.conf 是搜索其他目录的首选方式,而不是使用 LD_LIBRARY_PATH,因为这样更安全(将特洛伊木马库放入 /etc/ld.so.cache 比它更难)将其插入 LD_LIBRARY_PATH)。
  3. /lib, followed by /usr/lib. /lib,然后是 /usr/lib。

More details can be found here library handling可以在此处找到更多详细信息库处理

edit:编辑:

DllImport will fail if libc.so contains any unresolved symbols.如果libc.so包含任何未解析的符号,则 DllImport 将失败。 Please verify with请与

ldd -r -d libc.so

and make sure no unresolved symbols are present there.并确保那里没有未解析的符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM