C / C ++中的跨平台兼容性(从Java移植项目)?

时间:2023-02-15 12:20:08

I wrote a program in Java/JavaFX because I needed cross platform support. As I progressed, I needed to do some low level OS networking operations that aren't possible in Java. I started with Runtime.exec(), moved to JNA/JNI, and now I'm worried that maintaining OS compatibility for Java will end up being more work than just porting to a native applications.

我用Java / JavaFX编写了一个程序,因为我需要跨平台支持。随着我的进步,我需要做一些Java中无法实现的低级操作系统网络操作。我从Runtime.exec()开始,转移到JNA / JNI,现在我担心维护操作系统与Java的兼容性最终会比仅移植到本机应用程序更多。

What is the best way to do this?

做这个的最好方式是什么?

I know GTK+ is supported by *nix and windows so I think I'll use that for my UI. How difficult is it going to be to maintain different versions of my application for each OS? Can you recommend a starting point for writing something that needs to be cross platform but still has low level OS access? I have a lot of Java experience (and a good amount of C++ with OpenGL) but I've never had to care about anything but Linux before.

我知道GTK +受* nix和windows支持,所以我想我会将它用于我的UI。为每个操作系统维护不同版本的应用程序有多难?您是否可以建议一个起点来编写需要跨平台但仍具有低级操作系统访问权限的东西?我有很多Java经验(以及使用OpenGL的大量C ++),但我以前从未关心过任何事情。

Alternatively, is there a good way to build raw packets, send ICMP pings, and do other low level networking tasks from Java?

或者,是否有一种很好的方法来构建原始数据包,发送ICMP ping以及从Java执行其他低级别网络任务?

4 个解决方案

#1


2  

Your question is pretty general, so I will list some best practices here. I can narrow things down, if you edit your question accordingly.

你的问题非常笼统,所以我会在这里列出一些最佳实践。如果您相应地编辑您的问题,我可以缩小范围。

1- The most importing thing is the compiler and the build environment. This is probably trivial to choose, both Clang and GCC are supported on all major platforms. However, the build environment is a bit tricky. Just a single Makefile will not be good for all platforms. Instead, go for something like CMake or SCons. This will help you manage your source and generate appropriate makefiles on all systems easier.

1-导入最多的是编译器和构建环境。这可能是微不足道的选择,所有主要平台都支持Clang和GCC。但是,构建环境有点棘手。只需一个Makefile就不适合所有平台。相反,去寻找像CMake或SCons这样的东西。这将帮助您更轻松地管理源并在所有系统上生成适当的makefile。

2- You might want to build the same GUI on all platforms. In that case, you can use Qt or GTK+ as you mentioned.

2-您可能希望在所有平台上构建相同的GUI。在这种情况下,您可以使用Qt或GTK +。

3- File I/O. If you have a lot of File I/O, you might want to have a look at Boost File System.

3-文件I / O.如果你有很多文件I / O,你可能想看看Boost文件系统。

4- For all additional libraries you use, make sure they are supported on all platforms. In general, I suggest using libraries that are historically well maintained, like Boost.

4-对于您使用的所有其他库,请确保它们在所有平台上都受支持。一般来说,我建议使用历史上维护良好的库,如Boost。

5- Finally, hide platform specific code using the PIMPL Idiom.

5-最后,使用PIMPL Idiom隐藏平台特定代码。

If you want to go deeper in Cross Platform C++ programming, here is a good book: Cross-Platform Development in C++: Building Mac OS X, Linux, and Windows Applications.

如果你想深入学习跨平台C ++编程,这里有一本好书:C ++中的跨平台开发:构建Mac OS X,Linux和Windows应用程序。

#2


3  

Since your requirement for native code seems related to network packets, you may take a look at JPCAP. Note: there are two forks of JPCAP: the one hosted on SourceForge (package net.sourceforge.jpcap.net), that doesn't support sending, and the one I linked here (package jpcap, which was originally hosted at http://netresearch.ics.uci.edu/kfujii). I think it will be enough for most "low level" network operations (including listening on promiscuous mode, sending non-IP datagrams, ICMP, etc).

由于您对本机代码的要求似乎与网络数据包有关,因此您可以查看JPCAP。注意:JPCAP有两个分支:一个托管在SourceForge(包net.sourceforge.jpcap.net)上,一个不支持发送,另一个我在这里链接(包jpcap,最初托管在http:/ /netresearch.ics.uci.edu/kfujii)。我认为这对于大多数“低级”网络操作来说已经足够了(包括监听混杂模式,发送非IP数据报,ICMP等)。

#3


1  

Qt is probably the best choice for cross-platform GUI. I don't think GTK+ is a great choice if you want to target Windows (or Mac). However, maybe this has changed. It's been while since I last checked. Also see this post.

Qt可能是跨平台GUI的最佳选择。如果您想要定位Windows(或Mac),我认为GTK +不是一个很好的选择。但是,也许这已经改变了。我上次检查已经有一段时间了。另见这篇文章。

Boost and Poco are both good cross-platform C++ libraries. They also both provide networking libraries (boost asio and Poco Net).

Boost和Poco都是很好的跨平台C ++库。他们还提供网络库(boost asio和Poco Net)。

Sending ICMP ping requests is supported by both boost and Poco.

boost和Poco都支持发送ICMP ping请求。

I'm not sure if these libraries allow you to build your own Ethernet frames and IP packets though.

我不确定这些库是否允许您构建自己的以太网帧和IP数据包。

#4


0  

It is more of design stuff,

它更像是设计的东西,

  1. Try to use cross platform libraries wherever it is possible (ex: boost)
  2. 尝试在任何可能的地方使用跨平台库(例如:boost)

  3. Write platform specific code and write good abstraction layer on top of it.
  4. 编写特定于平台的代码并在其上编写好的抽象层。

ex: Windows/Signaling, Linux/Signaling, Signaling Here,

例如:Windows /信令,Linux /信令,信令在这里,

  • Windows/Signaling is windows platform specific signaling code
  • Windows / Signaling是Windows平台特定的信令代码

  • Linux/Signaling is Linux platform specific signaling code
  • Linux / Signaling是Linux平台特定的信令代码

  • Signaling is platform abstraction layer, compile time we can decide the platform
  • 信令是平台抽象层,编译时我们可以决定平台

#1


2  

Your question is pretty general, so I will list some best practices here. I can narrow things down, if you edit your question accordingly.

你的问题非常笼统,所以我会在这里列出一些最佳实践。如果您相应地编辑您的问题,我可以缩小范围。

1- The most importing thing is the compiler and the build environment. This is probably trivial to choose, both Clang and GCC are supported on all major platforms. However, the build environment is a bit tricky. Just a single Makefile will not be good for all platforms. Instead, go for something like CMake or SCons. This will help you manage your source and generate appropriate makefiles on all systems easier.

1-导入最多的是编译器和构建环境。这可能是微不足道的选择,所有主要平台都支持Clang和GCC。但是,构建环境有点棘手。只需一个Makefile就不适合所有平台。相反,去寻找像CMake或SCons这样的东西。这将帮助您更轻松地管理源并在所有系统上生成适当的makefile。

2- You might want to build the same GUI on all platforms. In that case, you can use Qt or GTK+ as you mentioned.

2-您可能希望在所有平台上构建相同的GUI。在这种情况下,您可以使用Qt或GTK +。

3- File I/O. If you have a lot of File I/O, you might want to have a look at Boost File System.

3-文件I / O.如果你有很多文件I / O,你可能想看看Boost文件系统。

4- For all additional libraries you use, make sure they are supported on all platforms. In general, I suggest using libraries that are historically well maintained, like Boost.

4-对于您使用的所有其他库,请确保它们在所有平台上都受支持。一般来说,我建议使用历史上维护良好的库,如Boost。

5- Finally, hide platform specific code using the PIMPL Idiom.

5-最后,使用PIMPL Idiom隐藏平台特定代码。

If you want to go deeper in Cross Platform C++ programming, here is a good book: Cross-Platform Development in C++: Building Mac OS X, Linux, and Windows Applications.

如果你想深入学习跨平台C ++编程,这里有一本好书:C ++中的跨平台开发:构建Mac OS X,Linux和Windows应用程序。

#2


3  

Since your requirement for native code seems related to network packets, you may take a look at JPCAP. Note: there are two forks of JPCAP: the one hosted on SourceForge (package net.sourceforge.jpcap.net), that doesn't support sending, and the one I linked here (package jpcap, which was originally hosted at http://netresearch.ics.uci.edu/kfujii). I think it will be enough for most "low level" network operations (including listening on promiscuous mode, sending non-IP datagrams, ICMP, etc).

由于您对本机代码的要求似乎与网络数据包有关,因此您可以查看JPCAP。注意:JPCAP有两个分支:一个托管在SourceForge(包net.sourceforge.jpcap.net)上,一个不支持发送,另一个我在这里链接(包jpcap,最初托管在http:/ /netresearch.ics.uci.edu/kfujii)。我认为这对于大多数“低级”网络操作来说已经足够了(包括监听混杂模式,发送非IP数据报,ICMP等)。

#3


1  

Qt is probably the best choice for cross-platform GUI. I don't think GTK+ is a great choice if you want to target Windows (or Mac). However, maybe this has changed. It's been while since I last checked. Also see this post.

Qt可能是跨平台GUI的最佳选择。如果您想要定位Windows(或Mac),我认为GTK +不是一个很好的选择。但是,也许这已经改变了。我上次检查已经有一段时间了。另见这篇文章。

Boost and Poco are both good cross-platform C++ libraries. They also both provide networking libraries (boost asio and Poco Net).

Boost和Poco都是很好的跨平台C ++库。他们还提供网络库(boost asio和Poco Net)。

Sending ICMP ping requests is supported by both boost and Poco.

boost和Poco都支持发送ICMP ping请求。

I'm not sure if these libraries allow you to build your own Ethernet frames and IP packets though.

我不确定这些库是否允许您构建自己的以太网帧和IP数据包。

#4


0  

It is more of design stuff,

它更像是设计的东西,

  1. Try to use cross platform libraries wherever it is possible (ex: boost)
  2. 尝试在任何可能的地方使用跨平台库(例如:boost)

  3. Write platform specific code and write good abstraction layer on top of it.
  4. 编写特定于平台的代码并在其上编写好的抽象层。

ex: Windows/Signaling, Linux/Signaling, Signaling Here,

例如:Windows /信令,Linux /信令,信令在这里,

  • Windows/Signaling is windows platform specific signaling code
  • Windows / Signaling是Windows平台特定的信令代码

  • Linux/Signaling is Linux platform specific signaling code
  • Linux / Signaling是Linux平台特定的信令代码

  • Signaling is platform abstraction layer, compile time we can decide the platform
  • 信令是平台抽象层,编译时我们可以决定平台