Slashdot Mirror


Portable Coding and Cross-Platform Libraries?

Bradee-oh! queries: "My brother and I were just commissioned to develop a large energy management system for a few big college campuses in the area. It will be written in C/C++. We know that in 6 months, when a preliminary test system will be installed, it will be running on NT/2000 servers. The software will be tested on NT for up to 12 months and a final version will run on NT a year after that. We also know that around that time, it will shift to *nix servers, and we're expected to account for that in development. The question is, what sorts of cross platform libraries will make this as painless as possible? I've never made it a point to code for 2 platforms at once in any language other that Java. Aside from the GUI, which we've already agreed to use QT 3.0 for, we specifically are looking for cross-platform libraries for multi-threading, serial port I/O, and network I/O."

"Ideal libraries would be open source and free, though those aren't as important as tested/stable/reliable. What are your recommendations? Anyone have experience writing for multiple platforms at once with threading, serial I/O, and network I/O all in mind? The ideal scenario would be to recompile on the new platform without changing a line of code - will this type of portability be possible?"

32 of 531 comments (clear)

  1. You have the answer by dda · · Score: 1, Informative

    You have the answer : Java has all what you need.

    1. Re:You have the answer by jawtheshark · · Score: 3, Informative

      Crippled? Hmmm, I don't know if that is true. Java in itself is an elegant language which has some limitations due to trying to support such a wide range of platforms transparently. You have to know what you are doing, that is all...but that counts for any language.
      Java code runs fast (really fast) in a lot of cases, mostly the only thing that "feels" slow is the GUI. You can really enhance responsiveness by including intelligent Threading. You can also avoid creating of throwaway objects, thus limiting Garbage collection.
      For the GUI, just don't use Swing if you don't need to... AWT is enough for simple frontends, yes it is not perfect, but it is faster. I suppose it is even possible to write your own native implementation linking to, for example, QT libraries or W32 libraries. Not portable, lot of work, but speed ensure.
      Some people tend to forget: using a vitual machine inherently takes some performance, but that is the price to pay for excellent multi-platform support. Note that it's still is better to do test on all the platforms you want to support, but the implementation is done once. Especially when using applications (not applets, because of browsers (non)support ), mult-platform Java applications tend to work astonishingly well in my experience.

      --
      Ahhh...the great dumpster continuum. Many a free computer will be found there. -- sowth (748135)
    2. Re:You have the answer by dda · · Score: 2, Informative

      I've worked on several projects using the Java platform, where the portability was one of the the major issue.
      I could also tell you that the major part of these applications were in a banking environment, where reliability and security were, of course, vital.
      It included Threading, I/O , GUI, ...
      The problem with Java is quite offen not the language itself, but the programmers which are not able to understand the basic
      O.O. principles, and to keep the logic as simple as possible.
      Furthermore, Java performances have been really improved since the version 1.0

    3. Re:You have the answer by bug1 · · Score: 2, Informative

      I didnt give a reason as i didnt think i would be questioned.

      Its interpreted.. or rather run time compiled, call it what you will it will _never_ be as fast as c.

      It requires a virtual machine, so it will always be more bloated than c.

      As the "portability" that you and others in this thread mention, gcc is portable. if you want portable code dont make it closed source.

      There are "feature-rich" libraries that can be linked to in c also.

      Java's only good point is its easy to program in, its the modern day equivalent of basic, or visual basic. Once you become a more advanced programmer, the benefit of being easy to programming in is of reduced benefit.

      Sun will never let java be really free.. they want to control it just as badly as any other large mega-corporation would, its just about money for them.

  2. A little known library is SFL by Hougaard · · Score: 3, Informative

    From www.imatix.com (The makers of the Xitami webserver).

    SFL is a great library for doing portable low-level stuff such as network etc..

  3. ACE ADAPTIVE Communication Environment by n-tone · · Score: 4, Informative

    ACE is an open-source framework that provides many components and patterns for developing high-performance, distributed real-time and embedded systems. ACE provides powerful, yet efficient abstractions for sockets, demultiplexing loops, threads, synchronization primitives.
    I've never tested this framework but it seems very good. I know several companies which use it and which are happy with it.

    1. Re:ACE ADAPTIVE Communication Environment by dkixk · · Score: 2, Informative

      I'll second the recommendation of ACE. In addition to ACE, The ACE ORB (TAO), Zen, their real-time ORB, and Jaws, An Application Framework for High Performance Web Systems, are all worth a look. This stuff has been around for quite some time and has been ported to nearly every compiler-platform imaginable.

    2. Re:ACE ADAPTIVE Communication Environment by Anonymous Coward · · Score: 1, Informative

      Hmm, I see two people recommending ACE, let me just add a vote against it. Before you decide on using it, go to the web page and read the documentation VERY thorougly. If you find it, that is. Seems most of the stuff just ran off some doc-tool that just threw together every other class declaration found. And there is little structure in (some parts of) that lib to begin with! I used ACE in some contract work, let me just say I didn't like it. I'd recommend looking at the ObjectSpace toolkits. These are not exactly cheap but depending on the project it might be worth it.

    3. Re:ACE ADAPTIVE Communication Environment by irix · · Score: 4, Informative

      I (and my company) also use this library extensively in a number of C++ projects to do multithreading, socket communications and memory maps, just to name a few things.

      It is open source, so it has the advantage that you can fix or patch bugs easily, and extend it if you want to.

      Even if you don't use the cross-platform part of ACE a lot, the C++ abstractions for some common UNIX paradigms are great to have - for example you can turn a class into a thread, etc.

      --

      Do you even know anything about perl? -- AC Replying to Tom Christiansen post.
  4. Usr Open source tools by Lumpy · · Score: 4, Informative

    Your choice of Qt is a good start, but be sure to use GCC, all open source libs that exist on both sides, and adhere as closely to the ANSI standard as you can. If you do the footwork now and gather the libs and other files needed now for both sides AND keep cross compiling on both platforms as you go.

    Dont think you can make it under NT and then 2 years later that it will compile on *Nix magically, you need to test every step of the way.

    Oh and be sure you have the libs that are identical for both platforms and freeze them before you start. nothing kills a cross platform project faster then changing libs and finding later that the libs you built for on platform X are no-longer compatable with platformY's libs.

    --
    Do not look at laser with remaining good eye.
  5. wxWindows by Anonymous Coward · · Score: 3, Informative

    wxWindows (http://www.wxWindows.org) supports quite a few platforms, is open-source, has been developing since 1992, and might be probably exactly what you're looking for.

    1. Re:wxWindows by joerg · · Score: 4, Informative

      wxWindows has Networking support (socket I/O, ipc,... whatever you want), thread support, a rich feature set for GUIs and much more (unfortunately there is no support for Serial I/O).

      You might even consider to drop QT in favor of wxWindows.

      It is availabe for Windows, XWindows (GTK based), Mac and some other platforms. It is released under the LGPL.

    2. Re:wxWindows by erlando · · Score: 3, Informative

      And it has the IMO great advantage over Qt in that it uses native widgets where possible. Qt draws its own widgets afaik.

      Also the Qt-license is very restrictive. wxWindows has a license explicitly allowing the library to be used for commercial purposes.

      wxWindows runs on Windows, MacOS (9 and X), GTK(+). Work is underway to implement a universal version of the library to be run on embedded platforms.

      wxWindows is open-source. Patches and contributions are highly encouraged. Also the mailing-lists are very active with several of the main developers taking part in the discussion.

      --
      Remember, there are no stupid questions. But there are a lot of inquisitive idiots.
  6. Re:GNU Compiler + CommonC++ by dyfet · · Score: 2, Informative

    GNU Common C++ certainly does abstract network I/O (sockets) as well as threads, serial I/O, XML parsing, and logging. There is also a supplimental RTP stack for it (ccRTP). It compiles native under win32 as well as under posix targets. It recently has had a very extensive rewrite, and is still recovering from that.

  7. At least use the STL.. by reachinmark · · Score: 3, Informative
    We have our own API that we used to support on both unix and NT. It was originally developed on IRIX, and we made extensive use of the C++ Standard Template Library which made things a bit easier when porting to NT.

    I realise that the STL isn't on the same level of cross-platform libraries that you were referring to, but it does aleviate some problems - and it's damn efficient too. Stream's, the way the STL uses them, can be quite an effective way of abstracting I/O.

    The biggest two problems we had was threading, and serial I/O. Threading, thanks to NT's posix libraries, wasn't that much of a pain - but we did need to write our own thread interface classes, one NT specific and one unix specific. Serial I/O was a little more tricky, but both came down to the same basic approach of treating the serial ports as files.

    I think the conclusion we came to was that it was a good idea to highlight the major differences first, and create your own wrapper classes around the OS specific methods. This way your main program is platform independant, and porting is kept to the wrapper classes.

    And of course, if you use the Cygwin environment, you shouldn't have any problems at all porting to another GCC platform.

    Only one warning: watch out for tricky C++ template uses, not all compilers support all features (e.g. Visual C++ lacks partial template specialisation amongst other useful features).

  8. NSPR by machingo · · Score: 5, Informative

    Check Netscape Portable Runtime, (just one of the many projects that are part of Mozilla. This library provide cross-platform threading, IPC, network/file IO, and dynamic linking, among other things.

  9. Apache Portable Runtime by bob@dB.org · · Score: 2, Informative

    would be a good place to start for the threading and networking parts. from what i've seen, it looks like a well engeneered package (and you'll have a hard time finding a better tested library :-). http://apr.apache.org/

    --
    Acts@core.mailboks.com Acrux@core.mailboks.com Adam@core.mailboks.com Adar@core.mailboks.com Ada@core.mailboks.com
  10. qt 3.0 have it all by eske · · Score: 2, Informative

    Well if you are using qt for GUI why not use the
    qt 3.0 network tools.
    se the doc for qt 3.0 http://doc.trolltech.com/3.0/

    I know that there is no IO mod but g++ have som you can use!

    --
    What rimes on recursion What rimes on recursion What rimes on recursion What rimes on recursion
  11. Rogue Wave by sql*kitten · · Score: 4, Informative

    You need SourcePro from Rogue Wave, formerly Threads.h++ etc. It provides a high level, easy to use C++ API for cross platform application development. Even if you're not going cross platform, the Rogue Wave stuff is excellent, provides loads of useful classes for threading, database access, network protocol handling and more.

    1. Re:Rogue Wave by Teancom · · Score: 3, Informative

      We have used .h++ for years around here for our main inhouse app. We decided just yesterday to remove all remaining dependencies and go to straight stl. Why? Because .h++ is not source-code compatabile with SourcePro, they provide no migration path, or even a "porting howto" (all according to our account rep). So we are faced with either spending the time and energy porting from .h++ to SourcePro, or spending the exact same amount of time and energy porting to the stl, with the side benefit of making our app linux compatable.

      Which reminds me, their linux port *SUCKS*. The latest and greatest .h++ will run on Redhat6.2, with gcc 2.95.2, the stock kernel, and *thats* *it* (not even perfectly, there are serious bugs). Any other platform (we've tried mandrake 7.2,8.0,8.1;debian2.1,2.2;redhat 7.0,7.2) will segfault/core dump. Their next release, due January, will run on redhat 7.1. I have no doubt that it will fail to run on anything else. So it really falls down wrt cross-platform compatability.

      So, to summarize, if you happen to be running one of the "tier-one" plarforms, their libs are pretty decent. Anything else, even if it's listed as "compatable", and you'll be screwed.

      (as a quick example of what I'm talking about, our app has different defaults depending on what it was called as, tc vs. frpt vs. etc, and it does a simple check of ARGV[0] to see if it .contains the various names. It doesn't work on linux. Flat-out returns the wrong value. And that's on redhat6.2. Any attempt to use their string class will immediately seg-fault. I'm seriously disappointed, in case you couldn't tell.)

  12. Ummm... POSIX? BSD Sockets? by Anonymous Coward · · Score: 1, Informative

    I was under the -- perhaps misguided -- impression that Win NT offers a POSIX subsystem. Combined with Qt (which does threads, IIRC), the serial port access is covered by POSIX, and network APIs are like UNIX (give or take a WSAStartup()/WSACleanup()).

  13. Re:Err, you told us didn't you? by Surak · · Score: 2, Informative

    Right. Serial port stuff is another issue. Most of the serial port stuff that I've seen written was either written years ago, or based off of stuff that was written years ago. What you'd have to do, more than likely is either find a serial port library that wraps around OS functionality, or write one yourself.

    'nix and NT handle serial ports very differently, and I doubt that such a library exists. Writing one should not be too difficult because serial port stuff is really basic anyways...you have data going in and data going out, it doesn't get more complicated than that. :)

  14. And why can't you use Java? by Anonymous Coward · · Score: 5, Informative
    You clearly point out that you have prior experience with Java. Java meets all of the requirements you listed:
    • Cross Platform
    • Portability without changing a line of code (or recompiling for that matter)
    • Multi-threading
    • Serial I/O
    • Network I/O

    In addition, you can get security, high availabilty, and scaleability with far less effort and platform-dependence than a C/C++ solution.

    Are you being forced to use C/C++ simply because of QT? Have you considered using QT in conjunction with Trolltech's QT AWT?

  15. Use G++ and Common C++ and use #ifdefs liberally! by Anonymous Coward · · Score: 1, Informative

    Use Common C++ for the cross-platform serial I/O, network I/O and threading support. I strongly suggest you use G++/cygwin rather than MS VC++ as the latter has all sorts of minor incompatibilities. G++ does too, but at least it's cross-platform. If you do decide to go with VC++, there's a few tricks you can use to make it closer to standard C++, such as #defining for="if (0) {} else for" (do it in the project pre-processor definitions if you don't want to #include it everywhere) which fixes the for-loop control variable scope problem in VC++. Any point where you do anything remotely Windows-centric when you're coding, at the very least put a comment, or put a #ifdef LINUX (or whatever) and flag an error when you try to compile on the other system, or better, figure out how to do it on the Linux system and write that in the #ifdef.

    It's very important to keep in mind that the system will be ported, and soon, otherwise it'll be a huge project to convert it when the time comes. It is probably worth doing things slightly less efficiently from the win32 perspective (use G++) so that the system can be easily ported later.

    Good luck!

  16. Re:Err, you told us didn't you? by larien · · Score: 3, Informative
    Even if the serial stuff was written years ago, is that such a problem? What difference is there between a serial port 10 years ago and a serial port now? My guess is not a lot, other than perhaps faster baud rates, but I don't think even that has changed in about 5 years (how long have we had UART 16550?).

    Worst case scenario, put all the serial code into one file and use functions like open_serial_socket(), read_serial_data which call the underlying OS functions. When you move to Unix, swap out the file for the new version.

  17. NT and POSIX by Anonymous Coward · · Score: 1, Informative

    One good thing is that NT is in the loosest sense POSIX compliant. That means, all the stuff like sockets, etc, work almost as you'd expect from Unix.

    Use something like QT for GUI, or glut if it's a predominantly 3D interface, and you'll have something that starts of being fairly platform independant.

    Use STL instead of MFC for storage. You may have to roll your own file loading/saving stuff, but don't try and reverse engineer the MFC stuff afterwards - it's not worth the hassle.

    I started off writing a network-based game targetted for Windows as while back. Pretty much everything was standard POSIX with OpenGL as the user interface. Before I started in earnest on the UI, everything ran under Unix - in fact, I wrote plug-in UI modules, as I eventually planned to back-port to Linux. That never happened, but I did have a text console UI. This made debugging very easy, as you can run lots of clients at once.

    If you're after socket stuff, check out the source for Xpilot. Not C++ style code, but it uses a library that you can look at to see some implementation differences between Unix and Windows.

    Definitely do daily builds on both platforms, gcc is good for cross platform use. You can even set up your Unix box to cross-compile your Windows binaries. If you target POSIX, you shold be able to isolate all the Windows/Unix stuff behind an abstraction layer early on and forget about it.

    Good luck!

  18. wxWindows by Anonymous Coward · · Score: 1, Informative

    Check wxWindows out http://www.wxwindows.org/, cross platform gui, i/o. Works under *nix, win32, os/2, macos etc

  19. wxWindows by Anonymous Coward · · Score: 1, Informative

    hi!

    i'd recommend wxWindows (http://www.wxwindows.org). that's a free (GPL, i think) cross-platform toolkit, or rather, a set of wrapper and supplementary classes.

    meaning, it'll use mfc on windows for displaying your application, gtk or motif on unices, etc, etc...

    the supplementary classes contain some network stuff, odbc client classes, yet another string class, config classes (using files or the windows registry), etc, etc.

    the documentation isn't that terrific, though, so there's a lot of guessing. and i'd download the cvs version, as that is pretty stable and contains a lot of features you'll be missing in the stable tarball.

  20. portable CORBA and threading: omniORB by matthew_gream · · Score: 3, Informative

    For portable CORBA and threading support, try omniORB (http://www.uk.research.att.com/omniORB/omniORB.ht ml). We use it for seamless portability between NT4.0 & Solaris components and its very effective.

    --
    -- Matthew - matthew.gream@pobox.com, http://matthewgream.net
  21. Re:Can it be browser based? by Anonymous Coward · · Score: 1, Informative
    You still have to worry about lower level stuff, but that's much easier to do properly if you eliminate the GUI.

    actually the GUI part is the least of his worries. several toolkits take care of that problem easily (for example, QT, which he settled on). network i/o is cake too. the real hurdle will be serial i/o (very different on the two platforms, but a small, well understood problem space) and threading (hard to get right on one platform, much less two).

  22. Java, baby by twdorris · · Score: 5, Informative

    Seriously. Most folks on here think Java sucks, but that's typically because they haven't actually *used* it. I think perl sucks, simply because I haven't used it and don't know it well enough to be efficient at it. That's all too common. But take a closer look at Java... It's cross platform, multithreaded, scaleable ('cept that pesky GC thing, which can be handled reasonably enough with intelligent coding), and does all the stuff you're looking to do.

    I used to code exclusively in assembly, then C, then C++, and now Java. Trust me, Java is a stable alternative that will solve MANY of these cross platform issues for you, if not all.

    As an example, I have written a datalogger for my car. I coded everything under Linux using vi. It's a Swing based app that's multithreaded and uses the serial port to communicate with the car's computer. I never gave a second thought to cross platform support. I just coded to the Java APIs.

    One day, a friend of mine wanted to run the thing on his laptop...which had Windows 98 on it. Sure enough, I put the files onto his laptop and it ran perfectly. This was not modified in any way and didn't even get recompiled! I just put the jar files from my Linux box onto this Windows laptop and away it went. I told him this "should" work, and sure enough, it did.

    As for scaleability, I have also helped design a VERY large scale middleware Java RMI server architecture for a VERY large shipping company (it's a public company...you know them...I'm positive you've used them). This handles all user-based load from their VERY large website. We're talking millions of transactions a day here, not thousands. With proper attention to garbage collection, multithreading, and a distributed architecture, this system runs without flaw, 24/7.

    So Java works in real world examples, it really does. Plus, it promotes code reuse so well, that I can't imagine suggesting any other solution for your problem.

  23. Depends... by Svartalf · · Score: 3, Informative

    If they're doing a lot of I/O device control directly with the app (Uh, this is an energy management system- which, by definition is going to be tickling things like Opto-22 panels, reading from sensors, etc...) then Java's only truely useful as a UI choice as they're going to have to come up with native interface code to drive the Opto-22 stuff.

    They not only have to do network programming and serial comms, they have to deal with industrial I/O that may/may not have a serial interface. If it does all have serial interfaces, they're going to have to come up with APIs for those devices- which isn't always easy.

    Java meets only part of the criteria- the ones they needed help with answers on. It doesn't magically meet the other criteria- what are they working with and what does the customer want. I suspect that Java doesn't make the grade here for some reason. I code in Java as well as C, C++, and Forth. I'd be using Forth or C/C++ for this sort of thing with maybe a CORBA driven UI coded in Java unless the customer requirements insisted on C/C++ for everything. Then I'd be using C/C++ because it's close enough to cross-platform to matter little if you pay close attention to your code. I know, I've been doing this sort of thing professionally for 7 years now.

    --
    I am not merely a "consumer" or a "taxpayer". I am a Citizen of the State of Texas