Writing "absolutely" portable code

  • Thread starter ArifulHossain tuhin
  • Start date
S

Seebs

Ah, let me guess, missing backslash? :)

Nothing so obvious! In the original it was Makefile variables, then we moved
to an SRPM package, and the SRPM package had
bash_cv_job_control_missing=no \
%configure [...]

and apparently this doesn't work, but changing it to "export" does. I don't
know the definition of %configure, but I'm guessing it was the underlying
mechanic.

.... I know none of this is really relevant to C, but it *is* somewhat relevant
to the question of why cross-compilation can be hard, and why I hate autoconf.
Stuff like this requires you to ensure that certain values are stashed in
environment variables or cache files, and that can be a nightmare to maintain.

I think experience with portability stuff like this is a great thing for any C
programmer. People who haven't had to deal with this are often quite quick to
carefully write multiple versions of something that each make unportable
assumptions; experience with this teaches the value of figuring out how to write
something that is portable in that it works everywhere, rather than in that it
works in three specific places. :)

I occasionally see people making things "portable" by doing things like:

#ifdef BIG_ENDIAN
#ifdef INT_32_BITS
if (((char *) x)[3] == 23) {
/* ... */
}
#else
if (((char *) x)[7] == 23) {
/* ... */
}
#endif
#else
if (((char *) x)[0] == 23) {
/* ... */
}
#endif

I would rather see:
if ((x & 0xff) == 23) {
/* ... */
}

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top