How to write a cross-platform program?

I was looking for some literature on cross-platform programming, and I did not find something really good.

I am not looking for a virtual machine for cross-platform, as Java does.

Is there any book or literature on this?

+6
cross-platform
source share
7 answers

@Staffan and others gave some win-win advice, so I'm not going to offer a solution - just suggest an approach.

There are many possible solutions, and I would say that the important solution is the question of whether you need a graphical interface. If not, you can go with C. With a graphical interface, switch to Java (which you don't want) or use NetBEans as your ID with Qt for widgets.

But no matter what you do, I think the most important thing is to discover potential weak points as early as possible. If you use C, you can use Splint or other static code analysis tools to warn about portability issues. Similarly, to help raise your compiler's warning level to max, follow these steps:

Set up a continuous integration system such as Hudson to constantly create everything and make sure you have a lot of autometd unit tests (using CppUnit or similar) and use the same test on each platform, with the tests aiming for portability problems .

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

How you configure CI depends on how many potential goals you have. You may need to run them each on a Sepearte computer or in a separate virtual machine. If you chose C or C ++, you may be satisfied with using GCC under Cygwin on Windows and the C / C ++ compiler of your choice (need some free advice?)

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

+4
source share

I would say that it comes down to:

  • Do not use non-standard functions of the “standard library”, where the non-standard depends on which platforms you are targeting (for example, POSIX-compatible systems). For example, if you use C, do not use popen() if you want your application to run on systems other than POSIX.
  • Make sure you handle endianess correctly where necessary, for example. when you transfer data over the network to another computer, possibly using a different byte order.
  • Writing only compatible code, for example. independent of the specific functions of GCC / VC / name-your-compiler.

At a more practical level, my advice is:

  • Use cross-platform libraries that abstract the custom functionality that you need, or write your code to handle all the platforms that you are targeting.
  • And, in connection with the foregoing, avoid disjoint platform dependencies. For example. use OpenGL instead of Direct3D and Qt / Gtk / whatever instead of your platform’s own widget toolkit.

EDIT: We need to think more, for example, not to assume that / used as a path separator or makes invalid assumptions about what characters are allowed in file names.

+7
source share

If you don’t use the "once run it anywhere" development environment (in other words, some of your codes must be configured for each target platform), then make sure that you really need your program to run several platforms.

I know a program that is compiled in two versions: one for Mac and one for Windows. Although most of the code was the same for any platform, part was specific to the O / S host, and often needed modifications and testing that were unique to both environments. Sales accounted for 98% of Windows, 2% of Mac, but the two versions essentially required equal development / programming / testing / documentation time. The owners decided that instead of giving half of every future programmer to the Mac version for only 2% income, they simply canned this version. Their cross-platform problems immediately disappeared, and the productivity of their programmers doubled significantly (which was liked by Windows clients).

+2
source share

I asked for a book, but no one called it.

I found a book called Cross-platform C ++ Development: Building Mac OS X, Linux, and Windows Applications.

Here is a link for Amazon and a description for those 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 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
+2
source share

Without using a virtual machine, you always face the same problem: many functions that you can use in the operating system are specific to this operating system.

This is because no one ever seriously cared to determine only a way to do the same, mainly because DirectX10, for example, is an advantage over other operating systems.

I would say it's pretty easy to write a cross-platform application that can run on MacOSX and Linux just because you can take advantage of many things that can work on both machines (think of X11 or GTK), and both of them are Unix under the hood. Usually, with some effort, you can make your programs work also under Windows (possibly with MinGW or Cygwin), also if some functions are not compatible.

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

The short answer is: no, you cannot live without the VM language, unless you limit the capabilities of your program to a small set of common functions.

(I assume that we are talking about C / C ++ only because usually only one group of people controls other languages, each of which takes care to free it as much as possible cross-platform)

In fact, I see nothing wrong with relying on a virtual machine. The computer is now able to run virtual machines without problems at high speed. Then, having something that takes care of placing the abstraction layer between you and the OS will allow you to do advanced things, just knowing how to do it. I think the trade-off between speed like Java is widely acceptable .. they just did what you would need to do when trying to write a complex cross-platform application.

Just a fact: it really hurts me when I try to port a virtual machine that I wrote from Unix (Linux / OSX) on Windows because of the freopen function. Unfortunately, Windows controls threads differently from other OSs, so MinGW lacked this feature, and I had to find a way to solve this problem.

+1
source share

There is no specific Java virtual machine that runs on different platforms. Java has different virtual machines that run on different platforms so that the Java program runs on each platform. This is a kind of layer that makes Java programs work everywhere.

For example, a C program can run on both Windows and Linux, but must be compiled with the appropriate OS compiler on which you are running the application.

But in java you don't need to do this. You simply compile the source code and can distribute compiled files that are ready to run on any platform.

0
source share

If you choose a modern scripting language (Tcl and Python are two examples), you can write programs that work almost the same on all platforms. Of course, you will have to make small discounts on one platform or another, but it is quite easy to do. Take a look at these languages ​​for some inspiration.

You will not create the next Photoshop or first-person shooter with these languages, but for the vast majority of ordinary programming tasks, they are quite suitable.

If you need something specific, check out http://tkdocs.com , which demonstrates Tk tools that work across multiple platforms and multiple languages ​​(Tcl, Ruby, Perl, Python)

0
source share

All Articles