Archived

This topic is now archived and is closed to further replies.

How do I preserve portability in my C++ programs?

This topic is 5772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What are some things I should avoid in order to allow my program to be portable to Unix/linux/etc without any conflicting errors or anything..(I''m on Windows with MSVC6).. For instance, what header files may not be supported all over, or functions, or i/o, etc..

Share this post


Link to post
Share on other sites
Once thing you can do is to wrap Windows specific functions inside your own functions and to make it easy to see where these functions are. That way anyone who is porting can just change these functions to replace the windows system calls to unix ones.

For example in the quake2 source you can look at the network code for windows in win32/net_wins.cpp and for linux in linux/net_udp.cpp

Both these files define identical functions like
void NET_SendPacket (netsrc_t sock, int length, void *data,netadr_t to)

but each implements each function differently using either winsock or unix sockets.

Share this post


Link to post
Share on other sites
There are tons of things you must know to maintain portability . Following most of the portability rules also improves your code though . Here is what the Mozilla organization recommends you do to write portable code. It''s next to impossible to follow all of those rules, but give it a shot .

Share this post


Link to post
Share on other sites
The Mozilla site is interesting, although I had to disagree with some with of the advice. It''s really summed-up in the first item that I came to: "Don''t use C++ templates". Arrrgh! The idea of omitting huge swathes of the language is really quite contentious. Then, I noticed, that the article was written in March ''98. That''s four years ago: a long time in the computer world. Interesting to note that this was before the Standard was completed; it''s probably fair to say that every platform has a reasonably compliant compiler these days.

In terms of portability of the C++ language and standard library, the Standard is the final arbiter of what you can expect from a compliant compiler, so it''s within reason to have those expectations. Of course, in the "real world", things don''t quite work that way so an attempt needs to be made to understand the shortfalls in the popular compilers. If portability is a serious requirement of a project, you should expect non-compliances in compilers and simpy plan for that as a fact of life.

--
The placement of a donkey''s eyes in its head enables it to see all four feet at all times.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
It''s really summed-up in the first item that I came to: "Don''t use C++ templates". Arrrgh!

I know what you mean; The only big reason I use C++ and not C is for the templates. That''s one of those suggestions that I opt out of following . There are many platforms without a compiler that supports templates still, but most mainstream ones have one (anything that has GCC ported to it ).

Share this post


Link to post
Share on other sites
Hey, come on, there are more reasons to use C++ over C than just templates! Actually, I''ve been pretty happy with the template support on the compilers by MS, IBM, HP, Borland and, in particular, Comeau (as well as GCC which you mentioned). It''s recognised that people actually want to use templates and have done for a few years. Anyone who hasn''t responded to this demand is worth ignoring. Comeau produce C++ front-ends for just about every platform you care to name, and they offer custom ports to any unusual platform, as long as it has a C compiler in place. As a point of interest, Comeau have recently released a beta which supports the "export" keyword. That makes them the first vendor to become compliant on all language issues (with the obvious exception of defects).

I think it''s safe to say the Mozilla article is well out of date as regards C++ compiler compliance.

--

The placement of a donkey''s eyes in its head enables it to see all four feet at all times.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
I''ve been pretty happy with the template support on the compilers by MS, IBM, HP, Borland and, in particular, Comeau (as well as GCC which you mentioned).


Err.. let me take that back somewhat. MS''s template support is really quite bad. I haven''t got around to trying v7.0 to check if they''ve sorted out the problems. I find it quite ironic when they have technologies like ATL and WTL leaning heavily on compiler template support. Still, I hear good things about v7, especially now Stan Lippman''s in charge.

--

The placement of a donkey''s eyes in its head enables it to see all four feet at all times.

Share this post


Link to post
Share on other sites
I know there''s more reasons, but most of them I could do more easily than I could do the macros (or complete rewrites if forced to) it takes to emulate templates. I do use namespaces, multiple inheritance, overloading (of functions and operators), et cetera; so I do recognize those things.

Share this post


Link to post
Share on other sites