Top 10 tips for code porting c/c++

Code portability basically refers to making source code able to compile on different platform without making any changes in source code.
While coding its very important to keep portability in mind.The best way to introduce code portability is while coding.Keeping certain things into account we can achieve code portability with lesser effort, which we will discuss in this post.There are certain tools too which detect portability problems on source code, its a post processing of code and requires extra effort.
Non-portable code introduces problems like maintenance of different versions, reduces readability, reduces understanding of code etc...
Efforts needs to make legacy or old source code portable, can really make you feel lost in this big programming ocean. So, the best policy is to keep portability into account while writing code, it saves lots of time and efforts on rework. Big question now is - "How to write portable code?".Our source code should be compatible with different environment like different processor, different OS, different version of libraries etc... In this post we would focus on basic tips need to be kept in mind while writing code.

1) Don't assume data type size to be constant across platform, as it may change with platform.

2) Don't use specific system constant.

3) System file/folder path notation may vary on different platform.

4) Avoid using system specific models/libraries.

5) Always write default statement in switch case.

6) Always specify return type for functions.

7) Always specify type with static variables.

8) Always take care of scope of variable.

9) Don't use C++ commenting style in C code.

10) Take care of include depth for header files and also for file code size.

I have tried to cover 10 basic tips for code portability for beginners though there are several other areas too, where we need to focus on advanced portability issues, for e.g. dealing with classes, virtual functions, exception handling, compiler directives, run-time identification.

Hope you enjoyed this post !

Keep Rocking
-Tajendra
http://tajendrasengar.blogspot.com/2010/03/how-to-achieve-code-portability-basic.html

5) Always write default statement in switch case.
Many latest compiler gives compilation error if default is not specified.

Um...what? Are you joking? That's like saying that compilers ought to give you an error if your if statement doesn't have an else statement following it.
Some compilers give a warning if you are switching an enum value and you don't process all the possible cases
A man is smoking a cigarette and blowing smoke rings into the air. His girlfriend becomes irritated with the smoke and says, “Can’t you see the warning on the cigarette pack? Smoking is hazardous to your health!”

To which the man replies, “I am a programmer. We don’t worry about warnings; we only worry about errors.”
And then the girlfriend asks "Really? Can I see some of your programs?"
"Sure!" says the programmer, thinking he will impress her with his intelligence.
So he shows her one of his programs. He runs it from the command-line and a window appears, and then dissapears instantly.
"Segmentation fault: core dumped" the girlfriend reads. "Well I guess the warnings really are there for a reason" she says.

"bitch go make me a sandwich!" he shouts
Last edited on
Its "bitch, go make me a sammich" don't you know? :P
You forgot the sudo.
sammich

Of course; but I wouldn't expect anyone else to.

You forgot the sudo.

It's k, I did a chown.
closed account (S6k9GNh0)
9) Don't use C++ commenting style in C code.

Isn't it the same in the latest standards? What can C commenting not do that C++ can?

And I say this because:
6) Always specify return type for functions.

This is from an OLD standard of C and C++. This has been gone for nearly two decades now. You must specify a return type or the function is given an error via compiler.
1) Don't assume data type size to be constant across platform, as it may change with platform.


Which is to say, never use "int" or "long" or "short" since none of these are guaranteed.

One of my pet peeves with C++. Calls itself portable, yet none of the standard integer types have the
same range on two platforms.
So you use use the C99 typedefs (uint{8/16/32}_t)?
How else can I guarantee the size, if size is important?

(yes, btw).

You could use your own typedefs with some preprocessor stuff...
Surely there's more to porting than that. No mention of how to deal with endian issues, build environments, system specifics, ... Do you litter your code with #ifdef <platform> or do you use some other way.

I'm sorry to say, but there's very little information of real use here.
Topic archived. No new replies allowed.