用C編寫跨平台應用程序 (Writing cross-platform apps in C)


問題描述

用 C 編寫跨平台應用程序 (Writing cross‑platform apps in C)

在用 C 語言編寫跨平台應用程序時,最應該牢記什麼?目標平台:基於 Intel 的 32 位 PC、Mac 和 Linux。我特別在尋找 Jungle Disk 在其 USB 桌面版 ( http:// /www.jungledisk.com/desktop/download.aspx )

這種類型的開發有哪些技巧和“陷阱”?


參考解法

方法 1:

I maintained for a number of years an ANSI C networking library that was ported to close to 30 different OS's and compilers. The library didn't have any GUI components, which made it easier. We ended up abstracting out into dedicated source files any routine that was not consistent across platforms, and used #defines where appropriate in those source files. This kept the code that was adjusted per platform isolated away from the main business logic of the library. We also made extensive use of typedefs and our own dedicated types so that we could easily change them per platform if needed. This made the port to 64‑bit platforms fairly easy.

If you are looking to have GUI components, I would suggest looking at GUI toolkits such as WxWindows or Qt (which are both C++ libraries).

方法 2:

Try to avoid platform‑dependent #ifdefs, as they tend to grow exponentially when you add new platforms. Instead, try to organize your source files as a tree with platform‑independent code at the root, and platform‑dependent code on the "leaves". There is a nice book on the subject, Multi‑Platform Code Management. Sample code in it may look obsolete, but ideas described in the book are still brilliantly vital.

方法 3:

Further to Kyle's answer, I would strongly recommend against trying to use the Posix subsystem in Windows. It's implemented to an absolute bare minimum level such that Microsoft can claim "Posix support" on a feature sheet tick box. Perhaps somebody out there actually uses it, but I've never encountered it in real life.

One can certainly write cross‑platform C code, you just have to be aware of the differences between platforms, and test, test, test. Unit tests and a CI (continuous integration) solution will go a long way toward making sure your program works across all your target platforms.

A good approach is to isolate the system‑dependent stuff in one or a few modules at most. Provide a system‑independent interface from that module. Then build everything else on top of that module, so it doesn't depend on the system you're compiling for.

方法 4:

XVT have a cross platform GUI C API which is mature 15+ years and sits on top of the native windowing toollkits. See WWW.XVT.COM.

They support at least LINUX, Windows, and MAC.

方法 5:

Try to write as much as you can with POSIX. Mac and Linux support POSIX natively and Windows has a system that can run it (as far as I know ‑ I've never actually used it). If your app is graphical, both Mac and Linux support X11 libraries (Linux natively, Mac through X11.app) and there are numerous ways of getting X11 apps to run on Windows.

However, if you're looking for true multi‑platform deployment, you should probably switch to a language like Java or Python that's capable of running the same program on multiple systems with little or no change.

Edit: I just downloaded the application and looked at the files. It does appear to have binaries for all 3 platforms in one directory. If your concern is in how to write apps that can be moved from machine to machine without losing settings, you should probably write all your configuration to a file in the same directory as the executable and not touch the Windows registry or create any dot directories in the home folder of the user that's running the program on Linux or Mac. And as far as creating a cross‑distribution Linux binary, 32‑bit POSIX/X11 would probably be the safest bet. I'm not sure what JungleDisk uses as I'm currently on a Mac.

(by DinahSteve WranovskydmityugovGreg HewgillAnthonyLambertKyle Cronin)

參考文件

  1. Writing cross‑platform apps in C (CC BY‑SA 2.5/3.0/4.0)

#32-bit #cross-platform #C






相關問題

內聯彙編 - cdecl 和準備堆棧 (Inline assembly - cdecl and preparing the stack)

來自 32 位用戶代碼的 64 位系統中的 ioctl 命令錯誤 (ioctl command wrong in 64 bit system from 32 bit user code)

Baiklah, PR: Bagaimana mungkin X[i] diinterpretasikan sama dengan i[X] di C? (Alright, homework: How can X[i] possibly be interpretted the same as i[X] in C?)

x32 ABI - гэта інструмент, як ім карыстацца (x32 ABI is this a tool ,how to use this)

Biên dịch Visual Studio 2012 32bit trên hệ thống 64bit (Visual Studio 2012 32bit compilation on 64bit system)

如何讓 Netbeans 7.2 使用 32 位 JVM (How get Netbeans 7.2 to use 32 Bit JVM)

反彙編代碼中的數組聲明 (Declaration of an array in disassembled code)

用C編寫跨平台應用程序 (Writing cross-platform apps in C)

為什麼 BinaryReader.ReadUInt32() 反轉位模式? (Why does BinaryReader.ReadUInt32() reverse the bit pattern?)

32 位 RHEL 機器上的內存使用 (Memory use on 32 bit RHEL machine)

將 32 位應用程序安裝到 C:\Program Files\ 而不是 C:\Program Files(x86)\ 會有什麼負面影響? (What would be the negative effects of installing a 32bit app into the C:\Program Files\ instead of the C:\Program Files(x86)\?)

Arduino 將浮點數轉換為十六進制 IEEE754 單精度 32 位 (Arduino convert float to hex IEEE754 Single precision 32-bit)







留言討論