using extern to have a global variable

This is a discussion on using extern to have a global variable within the C Programming forums, part of the General Programming Boards category; Hello, Compiling using gcc I am working on a project that has some modules that will share a common handler. ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    using extern to have a global variable

    Hello,

    Compiling using gcc

    I am working on a project that has some modules that will share a common handler. So I am using a extern

    I have created a cut down version of my project.

    However, I get an undefined reference hLibrary in library.c.

    Is this the correct way to do this using extern. Or is there a better way to share a global that is safer?

    Many thanks,

    library.h
    Code:
    #ifndef LIBRARY_H_INCLUDED
    #define LIBRARY_H_INCLUDED
    
    int open_library();
    
    #endif // LIBRARY_H_INCLUDED
    library.c
    Code:
    #include <stdio.h>
    #include "library.h"
    
    extern int hLibrary;
    
    int open_library()
    {
        printf("The library has been opened");
        
        //Set the value for this handler so it can be
        //used in another module (adapter.c)
        hLibrary = 1010;
    
        return 1;
    }
    adapter.h
    Code:
    #ifndef ADAPTER_H_INCLUDED
    #define ADAPTER_H_INCLUDED
    
    int find_adapter();
    
    #endif // ADAPTER_H_INCLUDED
    The module implementation file that will use the hLibrary handler.
    Code:
    #include <stdio.h>
    
    #include "adapter.h"
    #include "library.h"
    
    int find_adapter()
    {
        printf("\nStarting adapter");
        int hLibrary;
    
        if(hLibrary == 1010)
        {
            printf("\nAdapter has been started: %d", hLibrary);
    
            return 1;
        }
        else
        {
            printf("\nCould not find hLibrary");
        }
    
        return 1;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    to have a global var you need to declare it outside the function.
    extern just notifies compiler that there is such global var somethere else.

    if actually - there is no such thing - linker will be not able to find it later.

    Why do you need it as global? - pass it as a parameter
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    Thanks for the response.

    Working ok now.

    Apart from passing as a parameter. Is using extern like this a good style and safe to do?

    Thanks,

    Steve

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Is using extern like this a good style and safe to do?
    In my opinion - using globals never a good style

    Safe? from what? Multi-thread safe? definitely not.
    User-prone safe? definitely not. You have no control over who and where modifies your global data
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  2. extern variable
    By edesign in forum C Programming
    Replies: 4
    Last Post: 06-10-2009, 05:51 AM
  3. global struct variable best use
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 06-05-2009, 05:08 AM
  4. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 03:13 AM
  5. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM

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