There are many functions in CRT library marked as static. How it will be imported in modularized project? Here is the code reproducing the problem.
Header.h
static auto foo() -> void {}
ConsoleApplication5.cpp
import "Header.h";
int main()
{
foo();
}
The compiler output is:
1>C:\Users\serge\source\repos\ConsoleApplication5\ConsoleApplication5.cpp(9,1): error C2129: static function 'void foo(void)' declared but not defined
The same thing with /translateInclude compiler option and with data placed in anonymous namespaces. Is it a MSVC++ compiler bug or more common problem? So, how importing the static stuff can be performed?
In a modularized library you would replace your header with a cpp file, like so:
And then import that module from within your main compilation unit:
While you certainly can compile a header into a module unit, this is not the recommended approach.
static inlineis a compiler hack usually found in header-only libraries to guarantee ODR compliance. In modularized libraries you would not care about the name of the file where the function is defined, only the name of the containing module. You would also not mark the function as static as that would give it internal linkage, ie. not being a part of the module interface.Modules allow us to develop and distribute libraries without any header files, which will lead to cleaner code and a safer compilation.
[Edit]
If you are unable to modify the header, e.g. if it's a system header or part of a library, you have the option of
includingthe header in a module unit and provide wrappers around the functionality that you need:You do not actually need the namespace, but it might make for a cleaner interface.