Is there any way to use Glad within a DLL and exe? I feel like there may be problems since DLL's have their own memory space that is separate from the exe, but I'm not sure how I would go about refactoring my code to represent this.
My situation is that I have a game engine which contains numerous methods that use OpenGL functions that are loaded by glad, such as the shaders, textures, and anything else rendering related. But, I'm exporting the engine as a dll (for various other reasons), and the runtime editor links to that dll. This runtime editor requires access to the glad functions as well because it statically links to imgui, which needs the same window context and everything, and I'm assuming if I called gladLoadGL() in the dll and in the exe I would get different function pointers which means they wouldn't be operating on the same data. Is there any way to do this? I've tried linking GLFW as a DLL as well, but that doesn't help (which makes sense since this is a glad issue). If I don't do gladLoadGL() in my exe I get nullptr exceptions when trying to execute gl functions (because there are just a couple of cases where I use glFunctions inside the runtime editor). But if I do run gladLoadGL() in my exe, it loads new function pointers which throws everything off and I get errors inside ImGui, which I'm assuming is because glad has loaded different function pointers in the DLL and exe.
Can this be done, or do I need to rethink my overall architecture?
Edit: So my original question was answered, and you don't have to do anything special (except load the glad functions in the DLL and exe). However this raises a new problem within my code, which seems to be where the actual problem was. Right now I have a class in the DLL that looks something like this:
// In the DLL
class __declspec(dllexport) Application
{
Application();
void Construct(); // I added this because the base constructor would not be called
virtual void SomeFunc() {} // These are overridden by whoever inherits this class
virtual void SomeOtherFunc() {}
}
// In the exe
class MyApp : public Application
{
MyApp()
{
Construct() // This raises a runtime exception that says the method could not be found.
//Is there any way to make sure I export and import this correctly as well?
}
}
Edit 2: It turns out I was just missing a build step and never copying my dll to where the exe was located. Thanks again for all the help though
It's important to remember the distinction between a pointer variable and what it points to. If you have one instance of GLAD in a DLL and another in an EXE, then for each GLAD function pointer, you will have two pointer variables.
However, they will both point to the same function. They are after all loading function pointers for the same OpenGL context. So those pointers will point to the same functions.
So while you will technically be wasting some storage size (since the DLL and EXE both need pointer variables), it should still function just fine, so long as the OpenGL context is current when you load the pointers.