开发者

How to write a cross-platform program?

I开发者_如何学C searched for some literature about cross-platform programing development and I didn't find something really good.

I'm not looking for a Virtual Machine for cross-platforming like Java does.

Is there any book or literature about this?


I'd say that this boils down to:

  • Not using non-standard "standard library" features, where non-standard depends on what platforms you target (e.g. POSIX-compatible systems). For example, if you use C don't use popen() if you want your application to run on non-POSIX systems.
  • Make sure you handle endianess correctly where it needs to be, e.g. when you transmit data over the network to another computer possibly using a different byte order.
  • Write only compliant code, e.g. don't depend on GCC/VC/name-your-compiler specific features.

On a more practical level my advice is:

  • Use cross platform libraries that abstract the non-standard functionality you need, or write your code to handle all the platforms that you target.
  • And, related to the point above, stay away from non-cross platform dependencies. E.g. use OpenGL instead of Direct3D, and Qt/Gtk/whatever instead of your platforms native widget toolkit.

EDIT: There's more stuff to think about, like not assuming that / is used as the path separator, or making invalid assumptions about which characters are allowed in file names.


@Staffan and others have given sterling advice, so I am not going to offer a solution - just suggest an approach.

There are many possible solutions and I would say a major decision is whether or not you want a GUI. If not, you can probably go with C. With a GUI, go with Java (which you dion't want to), or use NetBEans as your IDE with Qt for the widgets.

But whatever you do, I think the most important is to detect potential pronlems as early as possible. If you use C, you can use Splint or other static code analysis tools they will warn of portability issues. Similarly, turnning your compiler warning levels to max should help.

Set up a continuous integration system like Hudson to build everything regularly and make sure that you have lots of autometd unit tests (using CppUnit or similar) and use the same test on each platform, with the tests targetting portability issues.

Perhaps consider Posix or using only Ansi C or some other standards compliant language.

How you go about setting up CI depends on how many potential targets you have. Maybe you need to run each on a sepearte PC or in a sparate VM. If you chose C or C++ you might be happy with using GCC under Cygwin in Windows and the C/C++ compiler of your choice (need advice on a good free one?)

See also:

http://www.hookatooka.com/wpc/

and, of course, http://www.google.com/search?hl=en&rls=com.microsoft%3Aen-sg&q=portable+code


I asked for a book, but nobody referred it.

I found a book named "Cross-Platform Development in C++: Building Mac OS X, Linux, and Windows Applications".

There's goes a link for Amazon and a description for who are interested:

http://www.amazon.com/exec/obidos/tg/detail/-/032124642X/

   Cross-Platform Development in C++ is the definitive guide to developing 
   portable C/C++ application code that will run natively on Windows, 
   Macintosh, and Linux/Unix platforms without compromising functionality, 
   usability, or quality. 

   Long-time Mozilla and Netscape developer Syd Logan systematically 
   addresses all the technical and management challenges associated with 
   software portability from planning and design through coding, testing, 
   and deployment. Drawing on his extensive experience with cross-platform 
   development, Logan thoroughly covers issues ranging from the use of 
   native APIs to the latest strategies for portable GUI development. Along 
   the way, he demonstrates how to achieve feature parity while avoiding 
   the problems inherent to traditional cross-platform development 
   approaches. 

   This book will be an indispensable resource for every software 
   professional and technical manager who is building new cross-platform 
   software, porting existing C/C++ software, or planning software that may 
   someday require cross-platform support. 

   Build Cross-Platform Applications without Compromise

   Throughout the book, Logan illuminates his techniques with realistic 
   scenarios and extensive, downloadable code examples, including a 
   complete cross-platform GUI toolkit based on Mozilla's XUL that you can 
   download, modify, and learn from. Coverage includes 
     - Policies and procedures used by Netscape, enabling them to ship Web 
   browsers to millions of users on Windows, Mac OS, and Linux 
     - Delivering functionality and interfaces that are consistent on all 
   platforms 
     - Understanding key similarities and differences among leading 
   platform-specific GUI APIs, including Win32/.NET, Cocoa, and Gtk+ 
     - Determining when and when not to use native IDEs and how to limit 
   their impact on portability 
     - Leveraging standards-based APIs, including POSIX and STL
     - Avoiding hidden portability pitfalls associated with floating point, 
   char types, data serialization, and types in C++ 
     - Utilizing platform abstraction libraries such as the Netscape 
   Portable Runtime (NSPR) 
     - Establishing an effective cross-platform bug reporting and tracking 
   system 
     - Creating builds for multiple platforms and detecting build failures 
   across platforms when they occur 
     - Understanding the native runtime environment and its impact on 
   installation 
     - Utilizing wxWidgets to create multi-platform GUI applications from a 
   single code base 
     - Thoroughly testing application portability
     - Understanding cross-platform GUI toolkit design with Trixul


If you don't use a "code once, run anywhere" development environment (in other words, some of your code has to be customized for each platform being targeted), then make sure you really need your program to run on multiple platforms.

I know of a program that compiled into two versions, one for Mac and one for Windows. Although most of the code was identical for either platform, a portion was specific to the host O/S, and frequently needed modifications and testing unique to the two environments. Sales were 98% Windows, 2% Mac, yet the two versions essentially required equal design/programming/testing/documentation time. The owners decided that instead of giving half of each $ of future programmer time to the Mac version for only a 2% return, they simply canned that version. Their cross-platform issues immediately disappeared, and the productivity of their programmers essentially doubled (which pleased the Windows customers).


There is no certain Virtual machine of Java that works on different platforms. Java has different virtual machines that run on different platforms in order for a Java program to run on each platform. It is kind of a layer which makes the Java programs be able to run everywhere.

For instance a C program can run on both windows and linux but it should be compiled with the corresponding compiler of the OS on which you run the application.

But in java, you don't need to do so. You just compile the source and can distribute the compiled files which are ready to be executed on any platform.


Without using a Virtual Machine you encounter always the same problem: many features that you can use on an operating system are specific for that operating system.

This because noone never seriously cared about defining just a way to do the same things, mainly because having DirectX10 for example is an advantage over other operating systems.

I would say that is rather easy to write a cross-platform application able to run on MacosX and Linux just because you can take advantage of many things that are able to run on both machines (think about X11 or GTK) and both of them have Unix under the hood. Usually with some effort you can manage to make your programs work also under Windows (maybe with MinGW or Cygwin) also if compatibility of some features is missing.

In addition the different operating systems have effectively different implementations for same things (think about sockets, io system, graphics, audio, etc) so this makes impossible to write just a version that works everywhere: you are forced to write a different version for any OS you plan to release your program for.

The short answer is: no, you can't without a VM language unless you restrict your program capabilities to a small set of common features.

(I'm assuming we're talking about C/C++ just because usually other languages are managed by just one group of people each that takes care about releasing it as much cross-platform as possible)

I don't really see any real bad point on relying on a Virtual Machine. Computer nowadays are able to run VMs without big speed issues. Then having something that cares about placing an abstraction layer between you and the OS will let you to do advanced things by just knowing one way to do it.. I think that a speed tradeoff of something like Java is widely acceptable.. they just did what you would have to do when trying to write a complex cross-platform application.

Just a fact: I really got into pain when trying to port a virtual machine I wrote from Unix (Linux / OSX ) to Windows because of freopen function.. unfortunately Windows manages streams in a different way compared to other OSs so MinGW lacked that function and I had to find a way around that problem..


If you choose a modern scripting language (Tcl and Python are two sterling examples) you can write programs that run almost identically on all platforms. Sure, you'll have to make small allowances for one platform or another, but that's pretty easy to do. Take a look at those languages for some inspiration.

You won't create the next photoshop or first person shooter with these languages, but for the vast majority of common programming tasks these are quite suitable.

If you want something concrete, take a look at http://tkdocs.com which showcases the Tk toolkit running on multiple platforms and with multiple languages (Tcl, Ruby, Perl, Python)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜