So C++20 promises the new modules feature, which is supposedly going to remove all the headaches of the current #include
system driven by the preprocessor.
The question is: what happens with old headers?
And this question can be split into 2:
1. What happens with standard library headers like <iostream>
?
I am pretty sure this is a very easy question to answer, the C++ standard must have taken care of this, but I am asking because I don't know how to do it. How would you replace the line:
#include <iostream>
with an import
statement?
2. What happens with other libraries that use headers, like <windows.h>
, <unistd.h>
and all the other third-party C/C++ libraries?
This one seems much more difficult to answer. In our good old #include
system, the library maker would compile the library into an object file or a static/dynamic library and publish it together with the .h
/.hpp
file containing the interface (you, as the user, would only have the source of the implementation of the library if it was an open source library).
Now, this new module system simply requires the library maker to compile the library and publish it. The rationale behind this (as I understand it) is that the linker will know what the library exports (that is all stored in the object file) and be able to link accordingly.
The problem arises when you have to use an old library that has an object file and a header file. It is safe to assume that most relevant library developers will convert their code to the new module system, but older (discontinued) libraries won't have that much luck. The object file will not have a module name inside it (because it wasn't compile as a module), so the code that uses the library will not have a module name to use. What should a library user do in this case?