Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross platform compatibility in C/C++ (porting a project from Java)?

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.

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.

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

like image 809
beardedlinuxgeek Avatar asked Mar 15 '13 03:03

beardedlinuxgeek


3 Answers

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).

like image 93
Javier Avatar answered Oct 20 '22 04:10

Javier


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.

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

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

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.

5- Finally, hide platform specific code using the 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.

like image 2
meyumer Avatar answered Oct 20 '22 06:10

meyumer


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.

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

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

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

like image 1
StackedCrooked Avatar answered Oct 20 '22 06:10

StackedCrooked