Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C, cross-platform portability and standards 1

Status
Not open for further replies.

DPisarev

Programmer
Dec 12, 2002
29
CA
Respected gurus,

I have a rather theoretical series of questions. I've been dabbling with C over the years but have finally come to the point where I need to resolve some fundamental questions... (It may help you to know that I come from the Java world.)

1) From what I understand, it is generally unfeasible to try to write C code that runs across all platforms that have a C compiler, like you write code in Java that covers all platforms by definition. For instance, from what I know, even such a thing as the number of bits in a primitive could vary between platforms in the C world. So, if I understand it right, if a programmer wants his C code to run on platform X in addition to his favorite platform Y, he has to explicitly review his code with platform X in mind and possibly insert some pre-processor instructions into the source code (to the effect: "if compiling on platform X, use this primitive or this class or library"). Is my understanding of this right?

2) If generic all-platform code in C is indeed unfeasible, how come we do talk of language C in general, in pan-platform terms? Is it just because a number of different platforms have adopted common standards, but still provide own implementations of those standards?

3) If it is indeed all about common C cross-platform standards, then where can one learn more about them?

4) Who defines those standards?

5) Could you please name some libraries that are standard and available on multiple platforms? I know of "the standard C library", naturally, and I suspect it fits in this category. I also suspect such things as socket-manipulation may also be pretty standard. Could anyone please clarify?

6) How similar is the situation in the C++ world?

Thanks in advance for your insights,
Dmitri Pisarev.
 
> Is my understanding of this right?
In part.
It's quite easy to write C which will run on any compliant platform, so long as you stick to only stdio type access to the outside world. Needless to say, that can get pretty boring.

> "if compiling on platform X, use this primitive or this class or library").
That's pretty much what you have to do - but there are ways of minimising the impact on your code.
Hypothetically, you would create
[tt]port_dos.c
port_win32.c
port_linux.c
port_mac.c[/tt]
Which contain all the bits which vary from platform to platform. As far as the bulk of your code is concerned, it is totally oblivious to which OS it is on, and just calls a function called [tt]foo()[/tt] say. Each port_xxx.c file implements [tt]foo()[/tt] in a variety of machine-dependent ways, but the interface and the overall effect remain the same across all of the platforms.

> 2) If generic all-platform code in C is indeed unfeasible,
Well a whole program is unfeasible (for most interesting programs), but there's no reason why most of the code in that program shouldn't be portable as a matter of course. After all, having only one file to port to a new OS rather than a few lines here and there in every single file has to be preferable right?
Otherwise, every single file will be a horrid mess of [tt]#ifdef #else #endif[/tt] constructs around every bit of dependency.

> Is it just because a number of different platforms have adopted common standards
Well the ultimate in portablility is to say that all you depend on is the ANSI standard. That basically limits you to the dozen or so header files in ANSI-C.
A good 2nd place is to say that your code depends on POSIX (Portable Operating Systems Interface). A whole slew of operating systems are compatible with the more general parts of POSIX, so you stand a reasonable chance of getting going on a new platform without too much trouble.

> 4) Who defines those standards?
ISO defines the C standard (ANSI-C is more correctly called ISO-C, since ISO is the international standard).
POSIX was defined by the IEEE originally, but it seems ISO is getting into that as well.

> 5) Could you please name some libraries that are standard and available on multiple platforms?
OpenGL is a cross-platform graphical interface.

> I also suspect such things as socket-manipulation may also be pretty standard. Could anyone please clarify?
They're in POSIX IIRC, though they first appeared in BSD.
If you have linux manual pages installed on your machine, then they usually have a "CONFORMING TO" section to help guide you as to how portable the function is likely to be.

> 6) How similar is the situation in the C++ world?
Well C++ has the likes of the STL and BOOST. The BOOST library has some things to hide the underlying OS from you, but you'd have to read up on that yourself.

--
 
Thanks a lot, Salem! Good food for thought.

Dmitri.
 
Re: using C++ across multiple platforms or even different versions on the same platform. You have to keep to a very basic subset. I have to use C and C++ on SunOS 5.6 to 5.9, HPUX 10.6, HPUX 11, Linux, Visual Studio 6.0 and Visual Studio 2003.net and they are all different. If you want something that builds on all platforms, you will need a configuration file: similar to the one that Rogue Wave use to configure their libraries on all platforms.


 
Thanks, xwb, I appreciate the comment about C++.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top