0

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?

0

1 Answer 1

2

In a modularized library you would replace your header with a cpp file, like so:

export module mylib;

auto foo() -> void {}

And then import that module from within your main compilation unit:

import mylib;

int main()
{
    foo();
}

While you certainly can compile a header into a module unit, this is not the recommended approach. static inline is 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 including the header in a module unit and provide wrappers around the functionality that you need:

module;

// Should include inside global module fragment
#include "Header.h"

// Export as a module unit
export module mylib;

// Declare wrapper
export namespace wrapper
{
    auto Foo() -> void { return foo(); }
};

You do not actually need the namespace, but it might make for a cleaner interface.

Sign up to request clarification or add additional context in comments.

2 Comments

But I can't change (at least it would be an ass pain) Windows SDK files (not CRT, my fault). But thanks anyway.
In that case, perhaps just remove static in front of all functions. I have experienced header unit compilation fails when functions were marked static. Dunno if you can change the Windows SDK files though, sounds bothersome. But you can always #include them inside the global module fragment of another module unit and provide wrappers around the functions that you need. Kind of a hack though, and it will also disable preprocessing configuration for whatever translation unit uses your wrapper. Modules and headers are not meant to work together long-term.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.