r/cpp_questions • • 4d ago

OPEN i'm having trouble with MSVC modules and static initialisation

UPDATE: I managed to narrow down code. Updated post body.

AFAIK, non-local variables exported from a module should be initialised before consumer of said module runs it's code; it was done to eliminate the notorious static initialization order fiasco. However, it doesn't seem to work in my project and I wonder if it's MSVC bug or there's something I'm missing:

// paths.cppm
export module paths;

import std;

export namespace paths
{
        const std::filesystem::path root_dir = []() {
                return std::filesystem::path(L"readme");
        }();
}
--------------------
// fileloader.cpp
export module fileloader;

import paths;
//import std; // reachable through paths?

export namespace fileloader
{

};

// this runs before paths::root_dir static initialiser!
static const bool init_files = []() { 
        for (const auto& entry : std::filesystem::directory_iterator(paths::root_dir)) {
                ;
        }

        return true;
}();
1 Upvotes

8 comments sorted by

3

u/sephirostoy 4d ago

I don't think modules have anything to do with initialization order.

What matters is the final binaries (exe, dll) where they are stored. If a.exe depends on b.dll then the global variables from b are initialized first when b.dll is loaded, then those in a are initialized when a.exe is loaded. However, within the same binary, the order are not controllable, it depends on the linker. 

2

u/FreakinApplePie2579 4d ago

I thought there was a guarantee (P1874R1) that any dynamic initialisers were to run in the order modules are imported. That's why I'm wondering. I've been asking AI before making this post, and it told me that "MSVC was historically quirky" in regards to this matter; recommended me to just use Meyers singletons.

I wanted to know if this is indeed a MSVC issue.

4

u/not_a_novel_account 4d ago edited 4d ago

I don't think modules have anything to do with initialization order.

This is wrong.

See 6.10.3.4, [basic.start.dynamic]:

(2) A declaration D is appearance-ordered before a declaration E if

  • (2.1) D appears in the same translation unit as E, or
  • (2.2) the translation unit containing E has an interface dependency ([module.import]) on the translation unit containing D,

in either case prior to E.

Defining static initialization order to fix SIOF was a significant goal of modules.

It's not arbitrary linker order anymore. Each interface unit creates a module initializer function which is called in interface-dependent order on program start.

1

u/sephirostoy 4d ago

Thanks for pointing me out this point, I was not aware of this design choice. 

2

u/CounterSilly3999 4d ago

Didn't get the question actually, just I think global/static variables are initialized not by calling some routine, rather by the loader -- fetching the values from the data segment set during compilation.

1

u/FreakinApplePie2579 4d ago

Maybe my understanding is wrong: so even if a static variable is initialised by a lambda, there's never a guarantee that this lambda runs before the value is read? not even with modules?

It seems P1874R1 was meant to solve this issue. Specifically Paragraph 3:

Run all global initializers from translation units which are transitively imported when they are imported.

4

u/ludonarrator 4d ago

Modules and MSVC quirks apart, global variables (non-constexpr) have always been a bad idea in C++, and it's also bad design/engineering in general.

0

u/not_a_novel_account 4d ago

The other comments here are wrong. Your understanding of modules is correct. This is an MSVC bug.