我如何使单声道在Linux上接受PE32 DLL

发布于 2025-02-09 07:35:23 字数 1097 浏览 2 评论 0原文

我正在尝试使用MCS单声道编译器在Linux主机上构建本机Win32应用程序。

我有一个具有以下行的库(coollib.dll),它

[global::System.Runtime.InteropServices.DllImport("MyLib.dll", EntryPoint="...")]

是使用mcs进行编译的,因此现在具有以下格式

$ file CoolLib.dll
PE32+ executable (DLL) (console) x86-64 Mono/.Net assembly, for MS Windows

mylib.dll也是我的,我用mingw-g ++在同一主机上编译。

$ file MyLib.dll
MyLib.dll: PE32+ executable (DLL) (console) x86-64, for MS Windows

然后,我编译了我的Hello World可执行文件,即引用coollib.dll ...

mcs /reference:CoolLib.dll /out:main.exe /target:x64 main.cs
MONO_LOG_LEVEL=debug mono main.exe

...但是它用system.dllnotfoundexception bulbed。

我不明白的是为什么它抱怨mylib.dll毕竟不是精灵

Mono: DllImport error loading library '/bindings_test/MyLib.dll': '/bindings_test/MyLib.dll: invalid ELF header'.

,但它应该是Win32库。为什么单声道期望它是精灵?

I'm trying to build a native win32 app on Linux host using mcs mono compiler.

I have a library (CoolLib.dll) that has the following line

[global::System.Runtime.InteropServices.DllImport("MyLib.dll", EntryPoint="...")]

It's compiled using mcs and thus has the following format

$ file CoolLib.dll
PE32+ executable (DLL) (console) x86-64 Mono/.Net assembly, for MS Windows

Now, MyLib.dll is also mine and I compiled on the same host with mingw-g++.

$ file MyLib.dll
MyLib.dll: PE32+ executable (DLL) (console) x86-64, for MS Windows

I then compile my hello world executable that references CoolLib.dll...

mcs /reference:CoolLib.dll /out:main.exe /target:x64 main.cs
MONO_LOG_LEVEL=debug mono main.exe

...but it blows up with System.DllNotFoundException.

Though what I don't understand is why it complains about MyLib.dll not being an ELF

Mono: DllImport error loading library '/bindings_test/MyLib.dll': '/bindings_test/MyLib.dll: invalid ELF header'.

after all, it's supposed to be a win32 library. Why does mono expect it to be an ELF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2025-02-16 07:35:23

从评论中复制。

您需要记住的一些事实是,

  • linux/macos本机可执行可执行文件是ELF格式(.SO和其他)
  • Windows本机可执行文件以PE32格式(.dll and .exe)
  • .NET组件(托管)为PE32 (.dll和.exe)

Microsoft决定重复使用托管位的相同文件扩展名和文件格式,因此,像您这样的许多人在移至另一个操作系统之前不会轻易注意到大更改。

因此,要回答您的问题,如果您有一些C/C ++依赖关系可以包装在Linux上,则必须使用本机C/C ++编译器以Elf格式重新编译它们。然后,您的.NET Core/Mono应用程序可以使用PinVoke消耗它们。您不能以PE32格式使用那些本机依赖关系,因为Linux无法识别那是什么。

Copied from the comments.

A few facts you need to keep in mind are,

  • Linux/macOS native executable is in ELF format (.so and others)
  • Windows native executable is in PE32 format (.dll and .exe)
  • .NET assemblies (managed) are in PE32 format (.dll and .exe)

Microsoft decided to reuse the same file extensions and file format for managed bits, so many people like you won't easily notice the big changes until moving to another operating system.

So to answer your question, if you have some C/C++ dependencies to wrap over on Linux, then they must be recompiled in ELF format using the native C/C++ compiler. Then your .NET Core/Mono apps can use PInvoke to consume them. You cannot use those native dependencies in PE32 format, because Linux won't recognize what's that.

捎一片雪花 2025-02-16 07:35:23

使用MCS生成的库是针对目标Intel 80386,而使用Mingw-g ++ 是针对目标x86-64 。

您无法混合平台。为32位或64位窗户构建所有内容。

例如,使用MingW-W64的32位版本使用32位版本,或使用-M32标志使用Mingw-W64版本支持这两个目标。

The library generated with mcs is for target Intel 80386 while the one built with mingw-g++ is for target x86-64.

You can't mix platforms. Build everything for either 32-bit or 64-bit Windows.

For example use a 32-bit version by using a 32-bit version of MinGW-w64 or use the -m32 flag if you have a MinGW-w64 version that supports both targets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文