How to write portable C++ code

In traditional meaning "writing portable code" means that you create a code that can be compiled and executed without major changes on several platforms (compilers/operating systems). It's important to know how to achieve this if you want your code to be compiled on many platforms. There are several techniques used for this purpose in relation to C++. Note that sometimes "portable" means just "portable across all Unix systems", especially when somebody mentions POSIX standard.

Portability guides

Compiler detection




Normally you need to compile C++ code for every major platform you want to execute it on. But with help of Emscipten you can convert your C++ program to JavaScript code and execute it on each platform with a web browser installed - without any recompilation.

See also

  • See undefined behaviour - you should eliminate all UBs from your code if you want it to be portable