Re: Portability / Conditional Compilation

From: Bjorn Reese (breese_at_see.signature)
Date: 02/25/05


Date: Fri, 25 Feb 2005 20:06:06 +0100

Michael B Allen wrote:

> However, I have been less successful in establishing a general strategy
> for dealing with portability using conditional compilation. How should a
> programmer reason about conditionally compiling for a feature when they
> may not have access to (or have the time to test on) a wide variety of
> platforms?

When I started http://predef.sourceforge.net/ I had two goals. The first
goal was to provide a comprehensive list of pre-defined macros. This is
what can currently been seen on the web pages. The second goal was to
provide header files that used the pre-defined macros to detect various
functions. This goal, had I had more time to fulfil, would have solved
your problems. Unfortunately, I have not had the time.

All is not lost, however. You can gain access to more platforms at HP's
compiler farms:

   http://www.testdrive.hp.com/

> When do you code to standards or architecture or compiler? If

I don't think there is a clear cut answer to this question. In general,
however, I start with the standards, then the platforms, and then the
compilers. Sometimes the include files will have additional macros that
you can use.

You can also look at other projects that use a similar approach, like
Boost and InfoZip.

> you code to a particular standard that's fine but the feature may still
> be available given a particular architecture / compiler. In many cases

If you provide a fallback solution to missing functions, then it does
not matter a great deal if you miss a single platform/compiler.

If you do not provide a fallback solution, then you must port your
software to the new platform/compiler (or get somebody to do that for
you.) Autoconf has the advantage that it may work on more platforms
than you have access to, but that is plain luck. I personally prefer
to know how to port to individual platforms/compilers; there may be new
issues that your autoconf stuff does not handle.

In the following I am going to skip the checks that I do not know.

> How would you conditionally check / define the following?
>
> dladdr many systems have this, any way to tell?
> snprintf semantics changed with C99

Use:

   #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)

Otherwise include http://ctrio.sourceforge.net/ in your project ;)

> mbrtowc how important is _REENTRANT?
> errno identifiers (e.g. ENODATA is not on OSX)

These are easy:

   #if defined(ENODATA)

> uint32_t pain because UNIX98 and C99 are different

This one can get quite complicated. Detecting and using UNIX98 and C99
are fairly easy. The pain comes from rest. You have to do something
like this (warning: incomplete)

   #define COMPILER_ASSERT(expr, msg) \
    typedef int ERROR_##msg[(expr) ? 1 : -1]

   #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
   #include <stdint.h>
   typedef uint32_t my_uint32_t;
   #elif defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 500)
   #include <inttypes.h>
   typedef uint32_t my_uint32_t;
   #elif defined(_MSC_VER) && (_MSC_VER >= 1100)
   typedef unsigned __int32 my_uint32_t;
   #else
   COMPILER_ASSERT(sizeof(int) == 4, Cannot_determine_uint32_t);
   typedef unsigned int my_uint32_t;
   #endif

Please note that I am using my_uint32_t to avoid problems in the two
last cases where uint32_t may be defined, but I was not able to detect
it.

> forkpty not standard but everyone seems to have it
> _T illegal I know but I want to use it anyway

IIRC then _T is a macro, so:

   #if defined(_T)

> union semun tough one
> variadic macros really only glibc and C99?
> backtrace glibc x86 only

I would find out in what version of glibc it was introduced, and then
use __GLIBC__ and __GLIBC_MINOR__ (along with something to detect x86)

-- 
mail1dotstofanetdotdk


Relevant Pages

  • Re: Removing "if (a = b)" warning
    ... various platforms I've now reached the state where there isn't a problem ... C allocated objects never cross 64KB boundaries. ... that the compiler has no direct knowledge of). ... It was one of their macros that I borrowed the cast to ULONG from :-) ...
    (comp.lang.c)
  • Re: Reflections on a classic Lisp Paper
    ... lisp dialects. ... user-defined special forms - Macros, Fexprs, and Nlambda. ... compilable because the compiler just couldn't do it. ...
    (comp.lang.lisp)
  • Re: "Sorting" assignment
    ... issue on some ancient compiler doesn't make a lot of sense. ... to his on a few commonly used platforms and compilers, ...  Be sure and call the swap ... reason to find algorithms which operate independent of it. ...
    (comp.programming)
  • Re: Cpp Considered Harmful
    ... > However, in either case, assertions are invaluable in making certain ... because we now have our own cpp ... > macros, we can use several different compilers and have our system ... > built into the compiler. ...
    (comp.lang.cpp)
  • Re: Death of Kylix and migrating towards Java
    ... But it can't beat Delphi's compiler speed! ... app developers on non-Windows platforms. ... It is NOT Delphi. ... > I do agree that Kylix was a very good promise and concept, that Borland ...
    (borland.public.delphi.non-technical)