1

I am compiling an existing C++ library as a C++/CLI DLL. The C++ library has two initialization routines which need to be called to set up various lookup tables.

The C++ library originally used std::once_flag and std::call_once() from <mutex> to perform one-time initialization of the lookup tables. However, in C++/CLI, the <mutex> header is not supported:
Error <mutex> is not supported when compiling with /clr or /clr:pure

I then tried using std::atomic_flag from <atomic>, but the <atomic> header is also unsupported in C++/CLI.

After seeing Where do I initialize a managed C++/CLI DLL? I then tried implementing DllMain(), but I ran into the problem that the C++ library's initialization routines needed to call managed code. In my case, I am not able to compile the initialization routines as unmanaged code (using #pragma unmanaged).

I then tried out a module initializer:

#pragma warning( disable : 4483 )
void __clrcall __identifier(".cctor")()
{
    InitLookupTables1();
    InitLookupTables2();
}

However, this causes 33 linker warnings of the form:

1>some.obj : warning LNK4210: .CRTMP section exists; there may be unhandled static initializers or terminators

I suppose that my custom module initializer is overriding a compiler-generated module initializer that properly runs all static initialization, such as setting global variables' initial values.

Is there a way to customize the compiler-generated module initializer or otherwise arrange to have the library initialization routines called by the compiler-generated module initializer?

Community
  • 1
  • 1
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • Why don't you just use static initialization? (Since you already are aware that the compiler-provided module initializer connects to that) – Ben Voigt Sep 24 '15 at 22:10
  • @BenVoigt: Do you mean precomputing `InitLookupTables1()` and `InitLookupTables2()` and setting the values of the global variables affected to the results? – Daniel Trebbien Sep 25 '15 at 11:38

0 Answers0