Can Objects Compiled with Different C Standards Be Safely Linked Together?
Suppose you have three compiled objects (A, B, C) from the same compiler version. They were compiled with different C standards: C 11, C 14, and C 17 respectively. However, the headers were all written in C 11 and used constructs with unchanged semantics across all versions.
GCC Considerations
For GCC, linking any combination of these objects is safe. The standard versions (i.e., -std options) are irrelevant since the objects are ABI-compatible if built with the same version of GCC.
However, problems arise if objects compiled with different GCC versions use unstable features from new C standards before GCC's support is complete. For instance, linking objects compiled with GCC 4.9 and 5 with -std=c 11 could cause issues as C 11 support was experimental in GCC 4.x. Similarly, linking objects from GCC 7 and 8 with -std=c 17 could cause problems due to the evolving experimental C 17 support.
In contrast, any combination of the following objects will work:
Since C 03 support is stable, the C 03 components are compatible across all objects. Objects E and F use stable C 11 versions, and Object D does not use C 11 features. Object F's C 17 features do not affect the other objects, as they share only C 03 or C 11 components. Compiling a fourth object, G, with GCC 8 and -std=c 17 would require recompiling F with the same version (or not linking to F) to ensure compatible C 17 symbols.
For programs using libstdc .so shared library, it must be at least as new as the GCC version used to compile any of the objects. For compatibility between objects using std::string, all objects should be compiled with the same string implementation. Use -D_GLIBCXX_USE_CXX11_ABI=0 for the original implementation, and -D_GLIBCXX_USE_CXX11_ABI=1 for the new implementation.
The above is the detailed content of Can Objects Compiled with Different C Standards Be Linked Together Safely?. For more information, please follow other related articles on the PHP Chinese website!