scope of global variables

This is a discussion on scope of global variables within the C++ Programming forums, part of the General Programming Boards category; I'm writing a bundle which is a plugin for a program. I have a global variable named say, x. There's ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    13

    scope of global variables

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    >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.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  2. Global scope and passing pointer
    By Dave++ in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2007, 03:52 PM
  3. Use global variables or pointers?
    By RealityFusion in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2005, 08:47 PM
  4. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM
  5. Global Variables?
    By Da-Spit in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2002, 08:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21