Thread: gcc and inlining

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    201

    gcc and inlining

    According to this article:

    http://gcc.gnu.org/onlinedocs/gcc/Inline.html

    gcc will not inline global functions even if they are declared inline.
    But it also says this:

    If you specify both inline and extern in the function definition, then the definition is used only for inlining. In no case is the function compiled on its own, not even if you refer to its address explicitly. Such an address becomes an external reference, as if you had only declared the function, and had not defined it.

    This combination of inline and extern has almost the effect of a macro. The way to use it is to put a function definition in a header file with these keywords, and put another copy of the definition (lacking inline and extern) in a library file. The definition in the header file will cause most calls to the function to be inlined. If any uses of the function remain, they will refer to the single copy in the library.
    anybody knows what that means? specify both inline and extern in function definition?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    inline extern void foo( void )
    {
        puts( "foo" );
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by quzah
    Code:
    inline extern void foo( void )
    {
        puts( "foo" );
    }

    Quzah.
    yes but if I do that the program doesnt even link. Bit confused about what the use is then

  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
    What linker error messages?

    Did you put the thing in a header file like the manual says, then include that header in all the places you need to refer to it?

    Did you create a library version for the rare instances where you have say a pointer to an inlined function (which obviously needs a real function doing the same thing)?
    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
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    oh yeah now I see. You have to duplicate the function in the header and a source file and put extern inline in the header and just normal function in source file.

Popular pages Recent additions subscribe to a feed