i was going through the OBS source to understand how they did the plugin system. I'm stuck on one part that i cannot reproduce and that i didn't find the answer anywhere.
To share the function with the plugin (dll) that OBS load, there is a pointer to an APIInterface
. This pointer is named API
so i'll call it like that from now. This variable is declare in OBSApi\APIInterface.h
and the declaration is: BASE_EXPORT extern APIInterface *API;
where BASE_EXPORT
is defined in OBSApi\Utility\XT_Windows.h
with
#if defined(NO_IMPORT)
#define BASE_EXPORT
#elif !defined(BASE_EXPORTING)
#define BASE_EXPORT __declspec(dllimport)
#else
#define BASE_EXPORT __declspec(dllexport)
#endif
Finally, in the the constructor of a OBS
object (in the main program) it initialize API
with API = CreateOBSApiInterface();
.
But, i cannot compile if there is a variable that i only declare & not initialize. So, i was wondering what i'm missing, how can this API
variable be shared among the plugin ?
Thanks for your answers.
What the plugin is doing in this case is using a global variable that is exported using __declspec(dllexport)
from the plugin into your program.
In your program it is imported using __declspec(dllimport)
.
The variable itself is defined within the plugin and the extern
tells your program that the declaration is not found in the current compilation unit, so it will be found when the linker goes off to look for it.
A small example of how this would look is as follows:
/* define the int* as an exported external. */
BASE_EXPORT extern int* exportedInt;
#define BASE_EXPORTING /* so the macro shown will export. */
#include "plugin.h"
/* actually define the int* so it exists for the linker to find. */
int* exportedInt = NULL;
#include "plugin.h"
/* use the int* in your program, the linker will find it in plugin.cpp. */
exportedInt = CreateIntPointerFunction();
The plugin could then be compiled as a static (.lib
) or dynamic (.lib
and .dll
) library which you can then link in with your code.
With a dynamic library you can choose to:
.lib
but you will need to manually find the values and entry points. .lib
, .dll
combo which will automatically tell the program where to find all the entry points and values.As long as you include the header file in the compilation unit and tell the linker where to find the .lib
when not doing runtime loading, you'll be able to use the exported functions and variables in your own program.