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?
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.
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.
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.
Related
My unity version is 5.3.4f, and I hava C# script as a.cs and compiled a.exe and it runs OK. Then I use IL2CPP to translate a.exe into C++ compiled exe a_il2cpp.exe with command:
il2cpp.exe --outputpath=a_il2cpp.exe --cachedirectory="obj_cache" generatedcppdir="generated_cpp" a.exe
But it shows no error and no output, do I miss something? I have C++ compiler installed.
Unity does not support using IL2CPP as a general mechanism for translating C# assemblies to native binaries. In theory this is possible (indeed some of the internal testing tools at Unity do this), but I don't think the command line you mention here will allow it to work.
I have a few questions about Mono:
1) Is there a way to compile application for Linux without installation of Xamarin Studio?
For example i have my helloworld.cs, i use dmcs to compile it into exe. Is there another compiler or flag to compile app for linux?
2) I need to statically link Mono runtime to run on the computer without Mono installed.
There is mkbundle command but it is a very difficult process to make it work on Windows. And static linking does not work on Windows.
And besides, I need to automate these processes =/
P.S. As I said, I'm working on Windows.
1) It doesn't matter if you compile your code with the Microsoft or Mono compiler, the resulting assembly should work on all platforms (as long as you don't use any platform-specific code of course).
You can take an assembly compiled with the MS compiler and run it on Linux/Mac with Mono, or compile on Mono and run it on Windows/.NET.
2) Problems with mkbundle should probably be filed as a bug on https://bugzilla.xamarin.com
I've written a C++/CLI DLL to be used with my GUI .NET application. On my local development machine, everything works as expected. My GUI application says that it cannot load my C++/CLI DLL on any other machine, though. It always says that it cannot load my DLL or one of its' dependencies. So I was thinking maybe it's some missing C runtime or something?
Are there any prerequisites that need to be installed prior to using my C++/CLI DLL on another machine? Strictly from a .NET perspective, or C++ run-time, or whatever.
Edit: Sorry. It's VS2012, .NET 4.0, Platform Toolset v110.
In addition to the dependencies that other .Net languages have (e.g., the .Net framework), C++/CLI requires the C++ runtime.
You can download the C++ runtime redistributable for VS 2012 from Microsoft. Select the x86 or x64 version based on the compilation setting of your C++/CLI assembly, not the version of Windows the target machine is running.
Note that this is the runtime for Release compiles only. Debug compiles use a different runtime, which does not have a redistributable, and is only installed with Visual Studio.
I have a python project that calls a c++ wrapper dll that calls a c# com interop dll.
In my computer, with all frameworks and programs installed, my project runs very well.
But in a computer that just got formatted it doesn't.
I allready installed c++ 2008 redistribute and the c++ part is working but when I call a function from it (that will call the c# correspondent one), it gives an error.
I want to know what are the dll dependencies from both c++ and c# dll's to see what is missing :)
Looks like you need Dependency Walker.
Dependency Walker (a.k.a. depends.exe) works for both native DLLs and managed DLLs.
It is included in some Visual Studio versions, and can also be downloaded here.
When I try to use LuaInterface on Mono on Linux (using Mono 2.0 on Ubuntu 9.04) I get the following exception:
** (App.exe:8599): WARNING **: Method ':.DoDllLanguageSupportValidation ()' in assembly
'/home/ulrich/test/Debug/lua51.dll' contains native code that cannot
be executed by Mono on this platform.
The assembly was probably created using C++/CLI.
According to this web site LuaInterface can be used with Mono. MoMA says that too.
Is it possible to recompile lua51.dll to make it compatible to Mono?
LuaInterface looks to be pure C#, but it uses a mixed mode C++/CLI-ified version of the Windows version of the native Lua library, that mixes .NEt code and native 32-bit Windows code. There's no C++/CLI compiler for platforms other than Windows, so you can't port/recompile the C++/CLI code, though it should work on Mono on Win32 (or maybe Wine)..
The only really viable way to get this to work on Mono would be to make it use P/Invokes istead of C++/CLI. You could then use a dllmap so that when Mono tries to resolve the P/Invoke calls to lua51.dll, it is redirected to the Linux equivalent, liblua.so.5.1.
Older versions of LuaInterface use a pure P/Invoke wrapper. You could use this.
There are also a few attempts at alternatives, my own included. http://github.com/jsimmons/LuaSharp
For all of you reading this now: Use KopiLuaInterface!
See my post here: https://stackoverflow.com/a/21386450/1070906