Thread: Help to remove the warning "x function defined but not used"

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    11

    Question Help to remove the warning "x function defined but not used"

    Hi all,

    I need a small help in removal of the warning "Function xxx is defined but not used".

    Actual Problem:
    =============
    There is a header file (say a.h) in which some functions are defined as static.

    This a.h file is included in 30 files.

    The object files of these 30 files are required to obtain the final output binary.

    There are only 3 files which uses all the static methods defined in a.h
    In remaining files only some of the static functions (defined in a.h) are used.

    When i compile the code with -Wall iam getting the warning :
    "function xxx defined but not used" for all of the unused static functions (defined in a.h)

    Reason for the warning:
    ==================
    We will get a local copy for each of the static methods when "a.h" file is included.(in c/cpp files)
    Only some of them (static functions) are used. So compiler issues warning for the un-used functions.

    Tried methods:
    ===========
    I tried by removing the static key-word for all the functions (for which the compiler issues warning).
    Then i was getting linker error:
    "There are multiple defnitions for function xxx".

    Could anyone suggest any way to remove these ("function xxx defined but not used") warnings?

    Thanks in advance....

    Regards,
    14341
    Last edited by 14341; 08-17-2009 at 07:15 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    1) Every compilation unit that includes "a.h" should use all the static methods to avoid the warning

    2) If you need to have the function implementation in the header, declare in "inline" instead. (This keyword has less to do with actual inlining, and more with telling the linker that it is OK to get more than one definition of the same identical function.)

    3) Preferably, headers contain declarations, and implementation belongs to an implementation file "a.cpp".
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You can do this:

    Code:
    #ifdef __GNUC__
        #define MAYBE_UNUSED __attribute__((used))
    #elif defined _MSC_VER
        #pragma warning(disable: Cxxxxx)
        #define MAYBE_UNUSED
    #else
        #define MAYBE_UNUSED
    #endif
    
    static inline void foo(void) MAYBE_UNUSED
    {
    }
    and add as many compilers as you wish. This is not very elegant, but better than eliminating warnings.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why on earth would you want to bloat the code with up to 30 copies of the function, most of which remain unused?
    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.

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Most examples are seen in optimization, combined with inline, written in headers. Cuz not many compilers have "extern inline" support. And of course these functions should be short.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    Thank you for the response...........

    Substituting the static keyword with inline worked for me.

    I am not a big fan of using the attributes (compiler specific).

    i have one more doubt.

    Is there any difference between inline and static inline ?
    Last edited by 14341; 08-18-2009 at 12:48 AM.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by 14341 View Post
    Is there any difference between inline and static inline ?
    Static inline can have different implementations in different compilation units. Just inline requires each compilation unit to include the same sequence of tokens.

    So if you have two different inline functions with the same name, namespace, and arguments, then using static will prevent them from clashing in an undefined manor. But really you don't want two such functions to exist.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    Thank you for the quick turn around.......
    Quote Originally Posted by King Mir View Post
    Static inline can have different implementations in different compilation units. Just inline requires each compilation unit to include the same sequence of tokens.

    So if you have two different inline functions with the same name, namespace, and arguments, then using static will prevent them from clashing in an undefined manor. But really you don't want two such functions to exist.
    Nope, i don't have any such [ same name, namespace, and arguments] functions in my header file.

    Some of the functions in my header file are static.

    Could you tell me if there is any difference in the way, the compiler (GCC) behaves if i made those static functions:
    1) inline (replaced static --> inline)
    2) static inline (replaced static --> static inline)

    Cheers,
    14341

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Actually, thinking about it more, there is another difference.

    If a function is static inline, but is not suitable for inlining, then each compilation unit in which the compiler decides it's unsuitable will have it's own copy of the function. Those compilation units where It is deemed suitable will be inlined. On the other hand, if the function is just inlined and deemed unsuitable for inlining, then any compilation units that function will use a common version of the function. I suspect most implementations will either fully inline, or don't inline at all, but it is possible to partly inline a function, in which case only the places that don't inline will use the single normal function implementation.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    11

    Question

    Thank you once again.......

    Quote Originally Posted by King Mir View Post
    Actually, thinking about it more, there is another difference.
    If a function is static inline, but is not suitable for inlining, then each compilation unit in which the compiler decides it's unsuitable will have it's own copy of the function.
    This means we will still get a warning ['function xxx' defined but not used] for the unused static methods defined in header files if they are unsuitable for inlining.

    Am i right?

    Cheers,
    14341

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You'll get that even if they are suitable. It's because they're static.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  4. Replies: 18
    Last Post: 12-31-2005, 01:56 PM
  5. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM

Tags for this Thread