Mixing C++ language versions
Can one mix compilation units or libraries built with different versions of C++? If I build my DLL with C++20, can a customer use it with their C++11 app? How are compiler and library vendors dealing with this? Do/will they supply multiple binaries for each C++ version? (I'm already building for each architecture plus release/debug. Adding the compiler version explodes the number of binaries to build and test.)
On 5/12/22 13:52, Kenneth Porter via Boost wrote:
Can one mix compilation units or libraries built with different versions of C++?
This question seems to be unrelated to Boost, but in general you should avoid mixing different C++ versions in one application. There was an ABI change between C++03 and C++11 at least in libstdc++. I'm not 100% sure there weren't any incompatibilities between other C++ versions, and I'm not following other implementations. There definitely were additions and changes in standard library APIs between C++ versions, though those are less likely to break ABI compatibility.
If I build my DLL with C++20, can a customer use it with their C++11 app?
It depends. If you're using C++20 features in public headers, those won't compile when included in a C++11 app. Also, your library will be linked to C++20 symbols in the standard library, which means the app will still require a C++20 standard library to link with.
How are compiler and library vendors dealing with this? Do/will they supply multiple binaries for each C++ version? (I'm already building for each architecture plus release/debug. Adding the compiler version explodes the number of binaries to build and test.)
Compiler vendors usually provide a single binary for all C++ versions (some vendors provide debug/release and static/shared versions). The binary exports all symbols for all C++ versions supported by it. The way they achieve this is through namespaces or other ways of different mangling of symbol names exported by the library. For example, you may notice libstdc++ exports symbols related to two versions of std::basic_string - one for C++03, one for C++11 onward. Library vendors deal with this as they see fit. Most, including Boost, don't go as far as libstdc++ does and simply use whatever symbols needed according to the C++ version they are compiled for. This means you'll have different binaries depending on the C++ version you build for. Whether those will be ABI-compatible or not, again, depends on the particular library in question. Maintaining ABI compatibility is a non-trivial task, not everyone is willing to pursue it. The best advice I can give in this regard is try to stick with one C++ version for the whole project. Possibly, the default one used by your compiler. If you need to mix different C++ versions, you have to explicitly ensure this combination is supported by the libraries.
On 5/12/2022 5:30 AM, Andrey Semashev via Boost wrote:
This question seems to be unrelated to Boost, but in general you should avoid mixing different C++ versions in one application.
I'm branching off from the current discussion about the minimum supported C++ language version for Boost. If Boost goes C++14 and my customer is still on C++03, I'm concerned about my DLL being compatible with their EXE. (I don't expose Boost or STL in my headers. Nor do I expose C++ strings. Allocations don't cross the EXE/DLL boundary, using factory constructors and destroy methods.) But I do worry about the ABI compatibility. According to MS, linking is allowed between compiler versions if the latest linker is used, but they don't say anything about language versions.
Kenneth Porter wrote:
On 5/12/2022 5:30 AM, Andrey Semashev via Boost wrote:
This question seems to be unrelated to Boost, but in general you should avoid mixing different C++ versions in one application.
I'm branching off from the current discussion about the minimum supported C++ language version for Boost. If Boost goes C++14 and my customer is still on C++03, I'm concerned about my DLL being compatible with their EXE. (I don't expose Boost or STL in my headers. Nor do I expose C++ strings. Allocations don't cross the EXE/DLL boundary, using factory constructors and destroy methods.)
But I do worry about the ABI compatibility. According to MS, linking is allowed between compiler versions if the latest linker is used, but they don't say anything about language versions.
Recent versions of MSVC (2015 onwards) only support C++14 and above. There is no C++03 or C++11 mode. Previous versions didn't have modes at all. I believe that MS does support linking between code compiled with different language versions (14/17/20). They also support linking code compiled with 2015/2017/2019/2022. This isn't true for earlier versions (and may be untrue for future versions too.)
participants (3)
-
Andrey Semashev
-
Kenneth Porter
-
Peter Dimov