Find the answer to your Linux question:
Results 1 to 4 of 4
Hi Friends, I have a variable defined in "local.h" file. local.h Code: #ifndef _LOCAL_ #defing _LOCAL_ static int MAX_CARRIER_SUPPORTED = 41; #define FRAGMENTS 200; ******* SOME MORE CONSTANTS OF #DEFINES ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    7

    Warning: Defined but not used

    Hi Friends,

    I have a variable defined in "local.h" file.

    local.h


    Code:
    #ifndef _LOCAL_ 
    #defing _LOCAL_ 
    
    static int MAX_CARRIER_SUPPORTED = 41; 
    #define FRAGMENTS 200; 
    ******* SOME MORE CONSTANTS OF #DEFINES ************ 
    #endif


    This file is been included in "SMCol.h" and "SMHoc.h"

    SMCol.h

    Code:
    #include "local.h" 
    int fun() 
    { 
    if(R1SR == 0) 
    { 
    MAX_CARRIER_SUPPORTED = 100; 
    } 
    else 
    { 
    MAX_CARRIER_SUPPORTED = 200; 
    } 
    }


    SMHoc.h
    Code:
    struct a 
    { 
    int *info; 
    a() 
    { 
    info = new int[MAX_CARRIER_SUPPORTED] 
    } 
    ~a() 
    { 
    delete[] info; 
    } 
    }

    but "SMCol.h" and "SMHoc.h" is been included in many files but those files in actual does not need anything to do with the MAX_CARRIER_SUPPORTED...

    and when i compile the project I am getting the warning saying that

    MAX_CARRIER_SUPPORTED is defined but not used.

    It has become a serious issue as this warning is appearing is many files during the build. :cursing:

    I am working on linux platform.

    Please friends help me.

  2. #2
    Linux Newbie
    Join Date
    Jan 2008
    Location
    UK
    Posts
    211
    Hi,
    It looks like its the problem of Local and Global variables and how the compiler treats them.
    Local is known only to the block of code in which they are declared
    and Global are known only to the file they reside.
    Either that or surpress the warnings if it annoys.

    I apologise if this is already known.

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    static int MAX_CARRIER_SUPPORTED = 41;


    try declaring

    int MAX_CARRIER_SUPPORTED = 41;

    without the static...this might help since your including MAX_CARRIER_SUPPORTED in other object files....

    Welcome to the Linux Forums

  4. #4
    Just Joined!
    Join Date
    Feb 2008
    Posts
    9

    Don't declare as static

    You are getting this warning because as gerard4143 has said
    when you declare a variable as static, its scope is limited to the file (.c file). So, what you have done is you have created multiple copies of this static variable in all the files where you included this .h file which declares this static variable.
    gerard4143 is correct that you should not declare it as static. But if you remove the 'static' what will happen is when you include this .h in multiple .c files, you will get a 'duplicate definition' error. So, what you should do is to make sure only one copy of this variable is present throughout your project.
    So, just 'declare' this variable using 'extern int..' in .h (and don't assign it in .h) and then define it (i.e. without using extern) in exactly one .c file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...