tags:

views:

413

answers:

4

What resources exist to aid in writing C/C++ code that works across multiple platforms and compilers? For example, I regularly find myself asking questions like:

  • Which preprocessor macros are automatically defined in various compilers and environments? (e.g. __GCC__, WIN32, __WIN32__, __CYGWIN__)
  • Which versions of compilers and standard libraries support relatively new "standard" functions (e.g. the C99 long double trigonometric functions sinl(), cosl(), ...)
  • What functions are available on different platforms for performing typical tasks, when no single portable function seems to exist? (e.g. getting the current time with sub-second precision)

I often write code that should compile under Linux/gcc, cygwin, mingw32, and Visual Studio, and I frequently have to compare notes from multiple sources (Linux man pages, MSDN, compiler docs) to get the information I need. This must be a problem that developers run into all the time -- are there any resources that compile this information into an easily digestible reference?

(For this question I'm not particularly interested in cross-platform libraries like wxWidgets or boost. I'm more interested in resources or techniques that would help somebody write their own cross-platform library or program.)

EDIT: This is an example of the type of page I'm looking for: http://predef.sourceforge.net/precomp.html. A nice survey of various compilers/environments and the preprocessor macros that can be used to identify them. It would be great to find a similar resource that compared nearly-equivalent functions across platforms (like gmtime_r() or ftime() in Linux vs _gmtime_s() or _ftime() in Windows) when no common function exists.

+1  A: 

The basic way you go about writing cross-platform code is to write code that doesn't depend on specific platforms. For example, almost all the original UNIX utilities can be written without reference to a specific platform. Writing code that depends on specific macros by using conditional compilation is not best practice.

anon
In principle I agree, and I do this whenever possible, but there are plenty of circumstances where there isn't a common function between platforms for some low-level task. For example, if you want to get the system time to millisecond precision, you could use ftime() on Linux, but on Windows it's called _ftime(). As another example, in a Linux build you might want your default configuration path to be set to ~/.myprogram, while on Windows it should be in %APPDATA%\MyProgram.
Kevin Ivarsen
+1  A: 

It really depends on your concrete project, but I would try to

  • stick to ISO compliant code and POSIX if possible;
  • use -Wall and eliminate all warnings;
  • check out autoconf and the autotoolset whether they can help.
Tobias
+4  A: 

Here is one resource that may be of use to you Pre-defined C/C++ Compiler Macros

Steve Fallows
Excellent find!
Kevin Ivarsen
A: 

Follow C/C++ standards.

http://www.open-std.org/jtc1/sc22/wg21/docs/standards

Partial