38,789
edits
(Updating to match new version of source page) |
(Updating to match new version of source page) |
||
Line 14: | Line 14: | ||
ISO standards typically rely on other standards and C++ is no exception. The ISO C++ standards rely on the ISO C standards definitions (per the text, restrictions, etc. in the ISO C++ standard). Consequently, if you require the ISO C++ standard document you will likely also require the ISO C standard document it relies on. | ISO standards typically rely on other standards and C++ is no exception. The ISO C++ standards rely on the ISO C standards definitions (per the text, restrictions, etc. in the ISO C++ standard). Consequently, if you require the ISO C++ standard document you will likely also require the ISO C standard document it relies on. | ||
=Well defined concurrency and memory models= | |||
=Well | |||
Prior to 2011 the ISO C++ standards had no definitions of concurrency and memory models in them, thus, in pre-C++11 compiled code there are no guarantees concerning the ordering of memory reads and writes under concurrency, i.e., such is likely undefined behaviour which the compiler vendor may or may not have documented. It is therefore preferable to compile concurrent C++ code as C++11 code (or newer). | Prior to 2011 the ISO C++ standards had no definitions of concurrency and memory models in them, thus, in pre-C++11 compiled code there are no guarantees concerning the ordering of memory reads and writes under concurrency, i.e., such is likely undefined behaviour which the compiler vendor may or may not have documented. It is therefore preferable to compile concurrent C++ code as C++11 code (or newer). | ||
=Compiler | =Compiler support= | ||
==Language | ==Language features== | ||
Various compilers implement various language features differently. Unfortunately a number of compiler releases only partially implement a specific ISO C++ standard. This can sometimes make it frustrating when compiling code with a compiler that does not yet implement a specific language feature. Fortunately there is a wiki page covering virtually all major C++ compilers and [http://en.cppreference.com/w/cpp/compiler_support listing the earlier compiler version implementing specific language features] at cppreference.com. This page also provides reference links to each compiler's web site concerning the details of such. | Various compilers implement various language features differently. Unfortunately a number of compiler releases only partially implement a specific ISO C++ standard. This can sometimes make it frustrating when compiling code with a compiler that does not yet implement a specific language feature. Fortunately there is a wiki page covering virtually all major C++ compilers and [http://en.cppreference.com/w/cpp/compiler_support listing the earlier compiler version implementing specific language features] at cppreference.com. This page also provides reference links to each compiler's web site concerning the details of such. | ||
==Standard | ==Standard library implementation== | ||
It is important to realize that many C++ compilers under Linux do not actually provide their own implementation of the C++ Standard Library under certain operating systems (especially Linux). Instead these compilers will use one that is normally installed on the system. Typically this implies that libstdc++, which is distributed with GCC, is used. | It is important to realize that many C++ compilers under Linux do not actually provide their own implementation of the C++ Standard Library under certain operating systems (especially Linux). Instead these compilers will use one that is normally installed on the system. Typically this implies that libstdc++, which is distributed with GCC, is used. | ||
Line 32: | Line 29: | ||
The GCC documentation has a section which details [https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html Standard Library components are supported in libstdc++]. | The GCC documentation has a section which details [https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html Standard Library components are supported in libstdc++]. | ||
=New to C++ or | =New to C++ or need an update?= | ||
If you are new to C++ or need an update then start by checking out the ISO C++ | If you are new to C++ or need an update then start by checking out the ISO C++ advocacy site's [https://isocpp.org/get-started Get Started] page --especially its recommended books. All of these books are excellent. (FYI, the Pearson/Addison-Wesley publications cited are available as watermarked eBooks without DRM from [http://www.informit.com/ www.informit.com] which is run by Pearson.) | ||
A book that specifically and only covers concurrency in C++11 (i.e., threads, atomics, condition variables, etc.) is published by Manning, [https://www.manning.com/books/c-plus-plus-concurrency-in-action C++ Concurrency in Action: Practical Multithreading]. | A book that specifically and only covers concurrency in C++11 (i.e., threads, atomics, condition variables, etc.) is published by Manning, [https://www.manning.com/books/c-plus-plus-concurrency-in-action C++ Concurrency in Action: Practical Multithreading]. | ||
=Pitfalls= | =Pitfalls= | ||
==The <tt>volatile</tt> | ==The <tt>volatile</tt> keyword== | ||
The reader should note that <tt>volatile</tt> in C++ has a very specific meaning, e.g., see [http://en.cppreference.com/w/cpp/language/cv this page]. Consequently needing to use <tt>volatile</tt> in C++ code is a rare event. | The reader should note that <tt>volatile</tt> in C++ has a very specific meaning, e.g., see [http://en.cppreference.com/w/cpp/language/cv this page]. Consequently needing to use <tt>volatile</tt> in C++ code is a rare event. | ||
Line 51: | Line 48: | ||
The GCC compiler's -O3 option includes possibly unsafe optimizations for some types of code (e.g., code relying on aliasing). If unsure, compile and optimize code using the -O2 option instead. If you've more time, read the man page (e.g., <tt>man g++</tt>) and unset the appropriate options by searching for "-O3" to see which options are turned on and turn off the settings that are not safe. | The GCC compiler's -O3 option includes possibly unsafe optimizations for some types of code (e.g., code relying on aliasing). If unsure, compile and optimize code using the -O2 option instead. If you've more time, read the man page (e.g., <tt>man g++</tt>) and unset the appropriate options by searching for "-O3" to see which options are turned on and turn off the settings that are not safe. | ||
====Linking | ====Linking with older previously compiled binaries==== | ||
The transition from GCC version 4.9 to version 5.1 introduced a major change to its ABI. If all source code including all dependent libraries is recompiled using the same version of the compiler then there will be no issues. If different compilers are used, the ABI change may cause linking to fail. The latter is likely to occur if you are linking to precompiled libraries provided in a vendor's product. If this occurs, you can use GCC's Dual ABI<ref>Free Software Foundation. The GNU C++ Library, Chapter 3. https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html</ref> feature to tell GCC to use the old ABI in order for your application to link properly with those legacy libraries, e.g., you would pass <code>-D_GLIBCXX_USE_CXX11_ABI=0</code> to GCC if using GCC v5.1 or higher to link to libraries built using the older ABI. | The transition from GCC version 4.9 to version 5.1 introduced a major change to its ABI. If all source code including all dependent libraries is recompiled using the same version of the compiler then there will be no issues. If different compilers are used, the ABI change may cause linking to fail. The latter is likely to occur if you are linking to precompiled libraries provided in a vendor's product. If this occurs, you can use GCC's Dual ABI<ref>Free Software Foundation. The GNU C++ Library, Chapter 3. https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html</ref> feature to tell GCC to use the old ABI in order for your application to link properly with those legacy libraries, e.g., you would pass <code>-D_GLIBCXX_USE_CXX11_ABI=0</code> to GCC if using GCC v5.1 or higher to link to libraries built using the older ABI. | ||