<p>What things should be kept most in mind when writing cross-platform applications in C? Targeted platforms: 32-bit Intel based PC, Mac, and Linux. I'm especially looking for the type of versatility that Jungle Disk has in their USB desktop edition ( <a href="http://www.jungledisk.com/desktop/download.aspx" rel="noreferrer">http://www.jungledisk.com/desktop/download.aspx</a> )</p>

<p>What are tips and "gotchas" for this type of development?</p>

[{'answer_id': 35704, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 35713, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 2, 'selected': False, 'text': '<p>Further to Kyle\'s answer, I would strongly recommend <em>against</em> 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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 35715, 'author': 'Steve Wranovsky', 'author_id': 3429, 'author_profile': 'https://Stackoverflow.com/users/3429', 'pm_score': 4, 'selected': True, 'text': "<p>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.</p>\n\n<p>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).</p>\n"}, {'answer_id': 35752, 'author': 'dmityugov', 'author_id': 3232, 'author_profile': 'https://Stackoverflow.com/users/3232', 'pm_score': 3, 'selected': False, 'text': '<p>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, <a href="http://oreilly.com/catalog/9781565920590/" rel="noreferrer">Multi-Platform Code Management</a>. Sample code in it may look obsolete, but ideas described in the book are still brilliantly vital.</p>\n'}, {'answer_id': 372045, 'author': 'Friedrich', 'author_id': 15068, 'author_profile': 'https://Stackoverflow.com/users/15068', 'pm_score': 0, 'selected': False, 'text': "<p>There do exist quite few portable libraries just examples I've worked within the past</p>\n\n<p>1) glib and gtk+</p>\n\n<p>2) libcurl</p>\n\n<p>3) libapr</p>\n\n<p>Those  cover nearly every platform and so they are extremly useful tool.</p>\n\n<p>Posix is fine on Unices but well I doubt it's that great on windows, besides we do not have any stuff for portable GUIs there.</p>\n"}, {'answer_id': 372071, 'author': 'AnthonyLambert', 'author_id': 31762, 'author_profile': 'https://Stackoverflow.com/users/31762', 'pm_score': 1, 'selected': False, 'text': '<p>XVT have a cross platform GUI C API which is mature 15+ years and sits on top of the native windowing toollkits. <a href="http://www.xvt.com" rel="nofollow noreferrer">See WWW.XVT.COM.</a></p>\n\n<p>They support at least LINUX, Windows, and MAC.</p>\n'}, {'answer_id': 372091, 'author': 'flolo', 'author_id': 36472, 'author_profile': 'https://Stackoverflow.com/users/36472', 'pm_score': 0, 'selected': False, 'text': '<p>I also second the recommendation to separate code for different platforms into different modules/trees instead of ifdefs. </p>\n\n<p>Also I recommend to check beforehand what are the differences in you platforms and how you could abstract them. E.g. this is some OS related stuff (e.g. the annoying CR,CRLF,LF in text files), or hardware stuff. E.g. the previous mentioned posix compability doesnt stop you from</p>\n\n<pre><code>int c;\nfread(&amp;c, sizeof(int), 1, file);\n</code></pre>\n\n<p>But on different hardware platforms the internal memory layout can be complete different (endianess), forcing you to use conversion functions on some of the target platforms. </p>\n'}, {'answer_id': 69182540, 'author': 'frang', 'author_id': 8875380, 'author_profile': 'https://Stackoverflow.com/users/8875380', 'pm_score': 0, 'selected': False, 'text': '<p>You can use NAppGUI for both console and desktop apps. The SDK uses ANSI-C and your code will work on Windows/macOS/Linux.</p>\n<p><a href="https://www.nappgui.com" rel="nofollow noreferrer">https://www.nappgui.com</a></p>\n<p>It\'s free and OpenSource.</p>\n'}]
['https://Stackoverflow.com/questions/35700', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/356/']