Re: Portability / Conditional Compilation
From: Bjorn Reese (breese_at_see.signature)
Date: 02/25/05
- Next message: Irrational Number: "makefile and TMPDIR interaction"
- Previous message: Mark Rafn: "Re: where does cc send the output by default"
- In reply to: Michael B Allen: "Portability / Conditional Compilation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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:
> 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
- Next message: Irrational Number: "makefile and TMPDIR interaction"
- Previous message: Mark Rafn: "Re: where does cc send the output by default"
- In reply to: Michael B Allen: "Portability / Conditional Compilation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|