是否存在 Java 到 C++ 转换器/工具?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/556807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:10:04  来源:igfitidea点击:

Does a Java to C++ converter/tool exist?

javac++code-translation

提问by n00ki3

I always asked myself if it would be possible to make a Java to C++ converter.

我总是问自己是否有可能制作一个 Java 到 C++ 的转换器。

Maybe a tool that converts the Java syntax to the C++ syntax?

也许是将 Java 语法转换为 C++ 语法的工具?

I am aware that the languages differ, but simple things like loops where the semantics match 1 to 1.

我知道语言不同,但简单的事情比如语义匹配 1 到 1 的循环。

Is there such a tool? Or is it possible to make one?

有这样的工具吗?或者可以制作一个吗?

采纳答案by cletus

It's possible to do anything given enough time, money and resources. Is it practical? Beyond trivial examples not really. Or rather it depends on what constitutes an acceptable error rate.

只要有足够的时间、金钱和资源,就可以做任何事情。实用吗?除了琐碎的例子,并不是真的。或者更确切地说,这取决于什么构成了可接受的错误率。

The real problem is that the idioms are different in Java to C++. Java to C# for example would actually be far easier (because the idioms are much more similar). The biggest of course is that C++ has destructors and manually managed memory. Java uses finally blocks for this kind of behaviour and has garbage collection.

真正的问题是 Java 和 C++ 中的习惯用法不同。例如,Java 到 C# 实际上会容易得多(因为习语更相似)。最大的当然是C++有析构函数和手动管理内存。Java 对这种行为使用 finally 块并进行垃圾收集。

Also Java has a common Object supertype. C++ doesn't.

Java 也有一个通用的 Object 超类型。C++ 没有。

The generics to templates would be nigh on impossible I would imagine.

模板的泛型在我想象中几乎是不可能的。

回答by Paul Whelan

http://www.tangiblesoftwaresolutions.com/Order/Order_Upgrade_Instant_CPlus_Java_Edition.htm

http://www.tangiblesoftwaresolutions.com/Order/Order_Upgrade_Instant_CPlus_Java_Edition.htm

Depends on the domain of where the code will be used, from a learning perspective perhaps it might be interesting.

取决于将使用代码的领域,从学习的角度来看,它可能很有趣。

i just found this via a google as I remembered seeing one in Univeristy that created code based on uml.

我刚刚通过谷歌找到了这个,因为我记得在大学看到过一个基于 uml 创建代码的。

回答by Johannes Weiss

Is is possible, no question, but it won't be so simple. It would be a Java compiler which generates C++.

毫无疑问,这是可能的,但它不会那么简单。它将是一个生成 C++ 的 Java 编译器。

If you want to do that from scratch, it will be very hard, you have to do all the work javac and the JVM do for you (e.g. garbage collection).

如果你想从头开始,这将是非常困难的,你必须完成 javac 和 JVM 为你做的所有工作(例如垃圾收集)。

Btw. Google has a Java to JavaScript compiler (included in GWT)

顺便提一句。Google 有一个 Java 到 JavaScript 编译器(包含在GWT 中

回答by John T

There are programs out there that claim they can do this, but none have gained enough popularity to be frequently mentioned, so we'll leave them at "attempts". Making a converter would require a lot of AI built into your program. The difficulty is increased tenfold when swing is involved because GTK/wxWidgets/Qt/win32 API all differ greatly from swing. But it is possible. Not that the code quality will be great, and no guarantees your program won't crash due to separate memory handling methods, but it's possible.

有一些程序声称他们可以做到这一点,但没有一个程序获得了足够的知名度以被频繁提及,因此我们将把它们留在“尝试”中。制作转换器需要在您的程序中内置大量 AI。因为GTK/wxWidgets/Qt/win32 API 都和swing 有很大的不同,所以当涉及到swing 时,难度会增加十倍。但这是可能的。并不是说代码质量会很好,也不能保证您的程序不会因单独的内存处理方法而崩溃,但这是可能的。

回答by cletus

As said it would be tough to convert Java to C++ but we can have an applicaiton or tool that generates code in Java and equivalnet C++ code.

如前所述,将 Java 转换为 C++ 会很困难,但我们可以有一个应用程序或工具来生成 Java 代码和等效的 C++ 代码。

I know one applicaiton which generates code in C++/Java/C# given a model which has its own way to deifine it.

我知道一个在 C++/Java/C# 中生成代码的应用程序,给定一个模型,它有自己的方式来定义它。

That tool belongs to CA and name is CA Plex. Search on www.ca.com

该工具属于 CA,名称为 CA Plex。在 www.ca.com 上搜索

回答by RzR

Something neat would be a tool , that translate java to "C++ using Java API" (like GNU GCJ CNI), one problem remain is to manage array.length(array not vector) ...

一些整洁的工具是将 java 转换为“使用 Java API 的 C++”(如 GNU GCJ CNI),仍然存在的一个问题是管理array.length(数组而不是向量)......

回答by Richard Smith

The main issue is that java is a language that is written and designed to talk to a VM. I suppose it would be possible, but all you would be left is a very poorly optimized application with a self translating layer doing what the VM already does. I mean, sure, it is possible, it still wouldn't be a solution for anything i could think of. If your looking to make your sluggish java app native, maybe your thinking too hard, just use an application like JET, its actually quite good, and will give you the benefits a native app would bring. Of course if the VM is already doing what the app is asking it to do just as well as native code could(it happens.. sometimes :P) it might change nothing.

主要问题是 Java 是一种为与 VM 通信而编写和设计的语言。我想这是可能的,但是你所剩下的只是一个优化得很差的应用程序,它有一个自翻译层,做虚拟机已经做的事情。我的意思是,当然,这是可能的,但它仍然不是我能想到的任何解决方案。如果您希望将缓慢的 Java 应用程序本地化,也许您想得太多了,只需使用像 JET 这样的应用程序,它实际上非常好,并且会给您带来本地应用程序带来的好处。当然,如果 VM 已经在做应用程序要求它做的事情,就像本机代码一样(它会发生......有时:P)它可能不会改变什么。

Java to c#, tho, sounds more reasonable, as both the languages are written in similar ways, talking to a framework as such, but this would still leave code very much unoptimized as code written from scratch for a particular framework can not be bested.

Java 到 c# 听起来更合理,因为这两种语言都是以类似的方式编写的,因此与框架对话,但这仍然会使代码非常未优化,因为无法超越从头开始编写的特定框架的代码。

回答by mitt10tim

Java to C would actually be the easiest. Remember you need to convert the language, If you do that, the required libraries can be converted by your new compiler. In other words Swing and AWT should not be a big problem...

Java 到 C 实际上是最简单的。请记住,您需要转换语言,如果这样做,新编译器可以转换所需的库。换句话说,Swing 和 AWT 应该不是什么大问题......

I would start by taking a good look at the Java Native Interface (JNI). The JNI is a part of java which allows it to be used with C and C++. The reason I would start here is that it becomes fairly obvious how parts of Java may be implemented in C. Once I had a grasp on basic structures, like how Java Objects can be mapped onto C structures (struct) and how pretty much everything in Java is an Object including arrays, I might peek at the Open JDK source code.

我将首先仔细研究 Java 本机接口 (JNI)。JNI 是 java 的一部分,允许它与 C 和 C++ 一起使用。我从这里开始的原因是,Java 的某些部分如何用 C 实现变得相当明显。一旦我掌握了基本结构,比如 Java 对象如何映射到 C 结构(结构)上,以及几乎所有的东西Java 是一个包含数组的对象,我可能会查看 Open JDK 源代码。

The actual converter would have to convert all the imported Java libraries (and their imported libraries and so on...) which means you would need the source code for everything. This conversion no small task since the Java libraries are large.

实际的转换器必须转换所有导入的 Java 库(及其导入的库等...),这意味着您需要所有内容的源代码。由于 Java 库很大,因此这种转换并非易事。

The process would be time consuming, but no AI should be required. However, I see no reason to perform a conversion like this. It looses the portability of Java and would not gain the efficiency of C (except that it would be compiled to native code, but it would be better to compile the machine code directly from the Java).

这个过程会很耗时,但不需要人工智能。但是,我认为没有理由执行这样的转换。它失去了 Java 的可移植性,也不会获得 C 的效率(除了它会被编译为本机代码,但直接从 Java 编译机器代码会更好)。

回答by mitt10tim

There is one, bit I am not sure if it actually works. Java to C++ Converter-Tangible Software Soulutions.

有一个,我不确定它是否真的有效。 Java to C++ Converter-Tangible Software Soulutions

It is weird how there are c++ to java converters, but only 1 java to c++ converter.

奇怪的是有 c++ 到 java 转换器,但只有 1 个 java 到 c++ 转换器。

回答by Thomas Mueller

The Firefox HTML5 parser is written in Java and converted to C++. But I think the converter used there is quite specific for this project. Interestingly, it turned out the resulting C++ parser was faster than the old parser written in C++.

火狐HTML5解析器是用Java编写并转换为C ++。但我认为那里使用的转换器非常适合这个项目。有趣的是,结果证明生成的 C++ 解析器比用 C++ 编写的旧解析器更快。

I'm also writing a converter as part of the H2 database, under src/tools/org/h2/java. The idea is to allow converting a subset of the H2 database to C++, so this is also not a general purpose translater.

我还在src/tools/org/h2/java 下编写一个转换器作为H2 数据库的一部分。这个想法是允许将 H2 数据库的一个子集转换为 C++,所以这也不是一个通用的翻译器。

And there is the open source project J2C.

还有开源项目J2C

So there are ways to convert Java to C++. But don't expect the translator support all features, and don't expect the resulting code to be any faster than a good Java JVM.

所以有一些方法可以将Java转换为C++。但是不要指望翻译器支持所有功能,也不要指望生成的代码比好的 Java JVM 快。