What are the things that I should keep in mind to write portable code? Since I\'m a c++ beginner, I want to practice it since beginning.
Thanks.
Use STL types when possible. Be careful of using system dependent types and APIs. For example don't use types like UINT, and DWORD on Windows.
You can use a library like boost to make it easier for you to write portable code. If you need a GUI consider using a cross platform toolkit like Qt.
Sometimes you will need to write platform specific code, and in those cases you can do something like this:
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
Write command-line programs to begin with. When you're ready, find a cross-platform windowing toolkit such as Qt.
If you're interested in writing multilingual code, use a third-party unicode library such as ICU rather than relying on platform-specific libraries.
Others said it before, but here is my view on it:
1) Do you need C++? It's not the best language for writing portable code because it's close to the bare metal. Java, Python, Perl, PHP or Javascript might be better for you.
2) If you need C++, don't try to write completely portable code, it's almost impossible anyway. Instead, decide early which platforms you want to support. For example: Linux, MacOS X, Windows
3) Make sure you test your code on all selected platforms continously. Don't just build on Windows and expect to just compile a Linux version 'when it's done'. Compile on all platforms daily and make sure you keep testing them for problems.
What are the things that I should keep in mind to write portable code?
a good idea is to use POSIX system calls. that way you don't have to deal with different ways of creating threads or using mutexes and signals.
the problem is that Windows is not exactly POSIX compliant, but there are libraries that implement certain POSIX features, like this one: [1]: http://sourceware.org/pthreads-win32/
Keep platform-specific code separate from reusable code, preferably in a different file but at least in a different function. If you start having #if WIN32
and #if CYGWIN
and #if BSD
all over the place you'll have a maintenance nightmare.
Then, compile on at least two different platforms early and often. Typical choices are Visual C++ on Windows and gcc on Linux. Since neither the system libraries nor the compiler is shared, you'll catch non-portable code before it becomes deeply entrenched in your design.