I'm writing a bundle which is a plugin for a program.
I have a global variable named say, x. There's another global variable named x in a different plugin. Is there a conflict, or is the variable only 'global' for the plugin?
Thanks
I'm writing a bundle which is a plugin for a program.
I have a global variable named say, x. There's another global variable named x in a different plugin. Is there a conflict, or is the variable only 'global' for the plugin?
Thanks
>Is there a conflict, or is the variable only 'global' for the plugin?
It depends. Can you give us some more info on how these plugins are written and used?
My best code is written with the delete key.
>It depends. Can you give us some more info on how these plugins are written and used?
Part of the problem is of course that I don't understand that fully! They're opened with dlopen(filename, RTLD_NOW). The variables in question are declared normally, (i.e. int x so no extern keyword.
There are functions which are declared extern "C", which do make use of the global variables.
Have you tried just replacing your plugin with a stub file with the definition of x and nothing else, and then compiling? If there's a conflict, you'll get an error.
> or is the variable only 'global' for the plugin?
I think that depends on how you create the library. IIRC, some librarians allow you to specify which symbols get exported out of the library. This is a good way of ensuring that only the API is exposed, and even poorly named globals within the library remain hidden.
Globals are terrible things to have, if you want to use the library more than once, say from different threads.
Consider an interface like
User *myLib_new();
myLib_delete(User *);
myLib_func1(User *, param, param);
myLib_func2(User *, param);
The User is an opaque type which contains all the 'global' variables the library uses. It solves the 'global' problem, makes it a lot easier to make the library thread-safe, and could help solve memory allocation/free problems (like the library allocates memory and stores that in a global pointer, with no obvious way of freeing that memory).
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.