Reputation: 31
This is what I want to achieve: build native C++ DLL library on Visual Studio, and invoke its' method on Ubuntu Linux\Mono via PInvoke from C# application. Simplified code:
[DllImport("MyLib")]
static extern int MyFunction();
static void Main(string[] args)
{
int result = MyFunction();
}
On Windows it works like a charm.
I run this sample application on mono, but I got error: DllNotFoundException
. When I enable mono debugging (MONO_LOG_LEVEL="debug" mono MyApp.exe
) then I can see that this DLL is found, but cannot be loaded because of error: "invalid ELF header". I suppose that DLL must be compiled with some special flags, so linux can recognize it. How to do this?
Upvotes: 2
Views: 1863
Reputation: 612954
The C++ compiler that is supplied with Visual Studio targets Windows. You are trying to execute code on Linux and so you need to compile your code with a compiler that targets Linux. You simply cannot execute a Windows DLL natively on Linux.
Your solution is to take the source code to a Linux C++ compiler and compiler a Linux shared object library.
Upvotes: 5
Reputation: 5969
According to Mono Documentation you have to link the DllImport
against X
and Mono should do the name translation.
For example:
[DllImport("X")]
static extern int MyFunction();
This line should link against X.dll
on Windows, libX.so
on Linux and Unix and X.dylib
on Mac OS X. You have to build each one on a native machine with gcc
or you a cross-compiler to do the work.
Notice that even though you compile it natively, if it makes use of native API (WIn32 API for example) it won't compile on other platforms. You have to use cross-platform development tools, libraries and patterns to work around these issues.
Upvotes: 0
Reputation: 20173
This won't work. The unmanaged library is specific to the platform on which you're running, so with Mono on Linux your unmanaged library needs to be a .so
.
Upvotes: 1