Can C/C++ software be compiled into bytecode for later execution? (Architecture independent unix software.)

前端 未结 6 908
失恋的感觉
失恋的感觉 2021-02-04 17:34

I would want to compile existing software into presentation that can later be run on different architectures (and OS).

For that I need a (byte)code that can be easily ru

相关标签:
6条回答
  • 2021-02-04 17:51

    As Ankur mentions, C++/CLI may be a solution. You can use Mono to run it on Linux, as long as it has no native bits. But unless you already have a code base you are trying to port at minimal cost, maybe using it would be counter productive. If it makes sense in your situation, you should go with Java or C#.

    Most people who go with C++ do it for performance reasons, but unless you play with very low level stuff, you'll be done coding earlier in a higher level language. This in turn gives you the time to optimize so that by the time you would have been done in C++, you'll have an even faster version in whatever higher level language you choose to use.

    0 讨论(0)
  • 2021-02-04 18:02

    The real problem is that C and C++ are not architecture independent languages. You can write things that are reasonably portable in them, but the compiler also hardcodes aspects of the machine via your code. Think about, for example, sizeof(long). Also, as Richard mentions, there's no OS independence. So unless the libraries you use happen to have the same conventions and exist on multiple platforms then it you wouldn't be able to run the application.

    Your best bet would be to write your code in a more portable language, or provide binaries for the platforms you care about.

    0 讨论(0)
  • 2021-02-04 18:03

    C compiled to LLVM bit code is not platform independent. Have a look at Google portable native client, they are trying to address that.

    Adobe has alchemy which will let you compile C to flash.

    There are C to Java or even JavaScript compilers. However, due to differences in memory management, they aren't very usable.

    0 讨论(0)
  • 2021-02-04 18:08

    LLVM is not a good solution for this problem. As beautiful as LLVM IR is, it is by no means machine independent, nor was it intended to be. It is very easy, and indeed necessary in some languages, to generate target dependent LLVM IR: sizeof(void*), for example, will be 4 or 8 or whatever when compiled into IR.

    LLVM also does nothing to provide OS independence.

    One interesting possibility might be QEMU. You could compile a program for a particular architecture and then use QEMU user space emulation to run it on different architectures. Unfortunately, this might solve the target machine problem, but doesn't solve the OS problem: QEMU Linux user mode emulation only works on Linux systems.

    JVM is probably your best bet for both target and OS independence if you want to distribute binaries.

    0 讨论(0)
  • 2021-02-04 18:08

    Web Assembly is trying to address that now by creating a standard bytecode format for the web, but unlike the JVM bytecode, Web Assembly is more low level, working at the abstraction level of C/C++, and not Java, so it's more like what's typically called an "assembly language", which is what C/C++ code is normally compiled to.

    0 讨论(0)
  • 2021-02-04 18:10

    There are several C to JVM compilers listed on Wikipedia's JVM page. I've never tried any of them, but they sound like an interesting exercise to build.

    Because of its close association with the Java language, the JVM performs the strict runtime checks mandated by the Java specification. That requires C to bytecode compilers to provide their own "lax machine abstraction", for instance producing compiled code that uses a Java array to represent main memory (so pointers can be compiled to integers), and linking the C library to a centralized Java class that emulates system calls. Most or all of the compilers listed below use a similar approach.

    0 讨论(0)
提交回复
热议问题