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.
-
How is using boost and wxWidgets not a strategy for writing cross-platform compatable code? It's like asking for something that tastes like grapes but isn't all "grapie".Kieveli– Kieveli2009年06月24日 17:01:45 +00:00Commented Jun 24, 2009 at 17:01
4 Answers 4
Here is one resource that may be of use to you Pre-defined C/C++ Compiler Macros
Comments
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.
1 Comment
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.
Comments
Follow C/C++ standards.