Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running C# code from C++ application (Android NDK) for free

I have a C++ game engine that currently supports Windows, Linux and Android (NDK). It's built on top of SDL and uses OpenGL for rendering.

One of the design constraints of this game engine is that the cost of development must be $0.00 -- building the engine should come at no cost to me (other than man hours), I must be allowed to freely redistribute the engine's code and binaries, and users should be able to sell games created using the engine with no restrictions.

Right now, I'm using a very slow interpreted scripting language for game logic -- it actually works well for writing glue code and simple responses to UI events, but not much else.

I'd like to replace this system with a C# solution -- have the user compile a C# class library (DLL) containing their game logic, and have the C++ side 'consume' this DLL and call the appropriate hooks.

It's been rather difficult to find information on how to achieve this in a cross-platform way. Each platform has a different way of hosting the needed runtimes. Also, most articles I've found suggest the use of full-fledged frameworks that provide platform abstractions that are already implemented in my engine.

Is there currently a way to run code from a C# DLL from an Android NDK-based C++ application without using an entirely different SDK, and without having to shell out hundreds of dollars for a license?

In particular, I'm eyeing some of Microsoft's recent open source .net initiatives -- anything there I could use?

EDIT: To clarify, Windows and Linux have well-documented ways of running .net code 'for free' -- this question pertains specifically to calling managed code from an Android NDK application WITHOUT paying licensing fees to Xamarin or another vendor.

like image 693
kiwibonga Avatar asked Jun 06 '15 18:06

kiwibonga


People also ask

What is running in C?

Step 3: Executing / Running Executable File (Ctrl + F9)We use a shortcut key Ctrl + F9 to run a C program. Whenever we press Ctrl + F9, the .exe file is submitted to the CPU. On receiving .exe file, CPU performs the task according to the instruction written in the file.

Can I run C on CMD?

We usually use a compiler with a graphical user interface, to compile our C program. This can also be done by using cmd. The command prompt has a set of steps we need to perform in order to execute our program without using a GUI compiler.

Can you run C on any computer?

A C program using nothing but standard C will compile (and THEN run) on any platform as long as you have a C compiler for it available. Show activity on this post. There is no such thing as a "C executable". A C compiler translates C source code to the specific target system.

Can Windows run C?

Two options. Great, now that Visual Studio Community is installed, you have two options for developing and running C programs on Windows. The first option involves using any text editor you like to write your source code, and using the "cl" command within the Developer Command Prompt to compile your code.


1 Answers

It's been rather difficult to find information on how to achieve this in a cross-platform way...

I'm only going to address the issue of "write once, run everywhere."

I build and maintain a library, composed of a single set of sources, that runs on Windows, Linux, OS X, Windows Phone, Android and iOS. To do that, I had to write everything in portable C/C++, and then build DLLs or shared objects.

The project uses .Net interop to call into the DLL, and Android uses JNI to call into the shared object. On iOS a static library is created rather than a shared object.

The DLL or shared object does have some platform specific defines. For example, it must know how to get random numbers from the underlying OS. So it will have a function like:

bool GetRandomNumbers(unsigned char* ptr, size_t size)
{
#if defined(WIN32) || defined(WIN64)
   ...
#elif defined(__linux___) || defined(linux)
   ...
#elif defined(__ANDROID___)
   ...
#endif
}

So keep your core business logic portable and in a DLL or shared object, #define where you must to abstract platform differences, and you will be OK.

On top of the portable library, you layer the platform specific GUI stuff.

If you try to write this in C# and then use that on other platforms, you're just going to be causing yourself problems. The only way to do portability with a single set of sources is to use portable C/C++.

I've also seen folks re-implement in every language: C and Win32 for Windows, .Net for Windows, C for Linux, Cocoa for OS X, .Net for Windows Phone, Java for Android and CocoaTouch for iOS. That creates about 6x the work, and the behaviors are never quite the same across platforms.

like image 175
jww Avatar answered Oct 14 '22 10:10

jww