1
\$\begingroup\$

If I create a C# console application in Monodevelop and build it I can run it by typing:

./executableName.exe

or using Wine. (I am using Ubuntu Linux)

I assume that this means that C# .exe files are cross-platform.

If so can I build a cross-platform Unity executable?

UPDATE:

Here is this program:

using System;

namespace MyConsoleApp
{
class MainClass
{
    public static void Main (string[] args)
    {
        Console.WriteLine ("This will run on Linux and Windows (I 
think).");
    }
}
}

When I build it I get this:

enter image description here

I can either run this file (MyConsoleApp.exe) by double clicking it which will open it in Wine:

enter image description here

Or by typing ./MyConsoleApp.exe :

enter image description here

This is why I think that C# programs are cross platform and wonder if I can build cross platform executables with Unity.

\$\endgroup\$
6
  • \$\begingroup\$ I think you can run files from the console by typing ./filename.exe because your os calls Wine to run .exe files. \$\endgroup\$
    – user100681
    Commented Dec 3, 2017 at 10:57
  • \$\begingroup\$ I am sure that I would be able to run these programs without Wine. \$\endgroup\$
    – xuwenbuwer
    Commented Dec 3, 2017 at 11:02
  • \$\begingroup\$ Since my answer would be too long, read about this here \$\endgroup\$
    – user100681
    Commented Dec 3, 2017 at 11:08
  • \$\begingroup\$ You can already export for linux, why do you need to add extra steps for users? Not everyone will install Wine for your game. \$\endgroup\$ Commented Dec 4, 2017 at 10:22
  • \$\begingroup\$ But what I am running on both Wine and Linux is the exact same executable. I know that different executables go on different Operating Systems, but doesn't C# generates maybe something similar to a .jar file which will run everywhere where interperenter is installed. \$\endgroup\$
    – xuwenbuwer
    Commented Dec 5, 2017 at 20:28

2 Answers 2

2
\$\begingroup\$

First of all you seem very confused. There is no way to create an executable file that can run on different platforms. If you read about Wine on their website, you'll find this:

"Wine (originally an acronym for "Wine Is Not an Emulator") is a compatibility layer capable of running Windows applications on several POSIX-compliant operating systems, such as Linux, macOS, & BSD. Instead of simulating internal Windows logic like a virtual machine or emulator, Wine TRANSLATES Windows API calls into POSIX calls on-the-fly"

So what wine does is translating the Windows API into Posix calls.

"I assume the C# .exe files are cross platform" No, they aren't. That's why different operating systems have different types of executable:

  • .exe Windows
  • .dmg MacOs
  • .deb .gz .tar LinuxOs

"If so can i build cross platform executables?" If you mean building a project in Unity and export it to different platforms then yes, you can. But you can't run natively an .exe file on an Os that is not Windows.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Also note that by emulating a program or translating the API at runtime(like Wine does), you are not gonna get the same performance as if you had the program running natively on your operating system \$\endgroup\$
    – user100681
    Commented Dec 3, 2017 at 9:56
2
\$\begingroup\$

I think the actual answer you are looking for is "sort of ... in a way". The way that C# works is less like normal (native) executables that you would run on your Linux or windows machine, and more like Java. With normal executables, your compiler compiles the code and produces a native executable, and then your OS loads that executable and runs it. However, in C#, you compile to CLI (common language infrastructure) code, then your Common Language Runtime (mono on linux, .net on windows) loads that code, and compiles the parts that it needs while it is running (JIT compilation).

So the platform dependent parts are taken care of by either mono or .net when you run the executable. The reason you can run the executable in linux using ./myprog.exe instead of mono myprog.exe is because the linux kernel has a feature called misc-executables, which basically sees that you are running a c# program and instead of trying to load non-native code, it loads an interpreter program (mono or wine depending on you your system is configured).

So I say that it is "sort of" cross platform because the native code is still platform dependent. But a c# application is not native code, so it actually can't run directly on any system (it will not run on my computer because I have neither wine nor mono installed). However, as long as a good enough CLR (common language runtime) implementation exists on the system, it should work. So it is cross platform in the sense that it can run on any CLR platform (because the CLR what does the loading and executing, NOT the OS).

Additionally, don't be confused by the program's extension (.exe), this does not denote the type of the file. People often see .exe and assume that means its a windows executable (PE executable), but in this case that is not true, its a CLI executable. Under linux, the extension is basically meaningless, linux (almost) completely ignores it. Linux determines if a program is an executable or not based on the executable flag in the file attributes. Most Linux native executables (ELF executables) do not have any extension. I believe though that (depending on how your system is configured), linux's misc-executable system might use the extension to determine that wine or mono is needed to execute your C# application.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .