Global unsafe static across dylibs

Hello everyone. I want to write a reflection library for a plugin-based game engine. Let's say I have a launcher that loads a number of dlls\sos. Obviously, the problem is that for every binary reflection database storage is different. How it it possible to define a "single static global point like in c++
" in rust?
I load plugins through libloading crate. And a plugin calls a macro which refers to a static database variable of the reflection lib(I know it is unsafe, but it will be called only on a main thread)
registration will be issued through ctor - Rust crate

Could you provide example C++ code of what you need?

If it helps, you can define an extern static variable in Rust and use it in much the same way that you would in C++.

Example from c++:
database.cpp

namespace
{

size_t g_globalCounter = 0;

}
size_t increment()
{
	return g_globalCounter++;
}

database.hpp

#pragma once

DLL_API size_t increment();

DLL_API is a _declspec(dllexport\import) stuff

Now, if I link it as dll to my main binary and I link it as dll to my plugins, this "database" state will be the same across dll so\dll boundaries

How would I achieve a similar functionality in rust?

Maybe there is a way to create a singleton,which will be unique across shared objects?

You can put the global in a separate dylib that you depend on from all other dylibs.

Thanks everyone. Seems like the issue was that at some point I was not using prefer-dynamic and thought static in dylib is different for every lib

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.