Function delcared inline but not defined inline

Discussion in 'C Programming' started by Nish, Oct 8, 2004.

  1. Nish

    Nish Guest

    Hi,

    I have a question regarding inline functions in C. If I declare a
    function as inline in the header file but do not define it there.
    Then I define that function in .c file, however neither make it static
    nor inline nor extern in the .c file.
    What would the compiler (gcc) do in this case? Will it just create
    object code for that function which can be called from any translation
    unit and never inlined. Or will it create the object code for that
    function, however inline it when I compile the .c file? Or neither of
    these?
    It would be great if someone could guide me to the correct group to
    post this message, if this is not the right group.

    Thanks,
    Nish.
     
    Nish, Oct 8, 2004
    #1
    1. Advertising

  2. Nish

    Jack Klein Guest

    On 7 Oct 2004 18:07:11 -0700, (Nish) wrote in
    comp.lang.c:

    > Hi,
    >
    > I have a question regarding inline functions in C. If I declare a
    > function as inline in the header file but do not define it there.
    > Then I define that function in .c file, however neither make it static
    > nor inline nor extern in the .c file.


    Under the 1999 C standard, when inline functions were added to the
    language, the code is illegal.

    6.7.4 para 6 says:

    "Any function with internal linkage can be an inline function. For a
    function with external linkage, the following restrictions apply: If a
    function is declared with an inline function specifier, then it shall
    also be defined in the same translation unit."

    Since this is a 'shall' outside of a constraint clause, the result is
    undefined behavior and no diagnostic is required.

    > What would the compiler (gcc) do in this case? Will it just create
    > object code for that function which can be called from any translation
    > unit and never inlined. Or will it create the object code for that
    > function, however inline it when I compile the .c file? Or neither of
    > these?


    As for what gcc will do, you would have to ask in one of the gcc
    groups (news:gnu.gcc.*). gcc has had inline functions as an extension
    before they were added to the C standard, with some differences from
    the way they work in some situations than those in the newer C
    standard.

    In any case, since such a program produces undefined behavior, gcc or
    any other compiler is free to do whatever it likes, since the C
    standard imposes no requirements at all once undefined behavior is
    involved.

    > It would be great if someone could guide me to the correct group to
    > post this message, if this is not the right group.


    This is the right group for asking about standard C language features,
    such as inline functions, in general. If you are specifically
    interested in what a particular compiler will do in a situation not
    defined by the C standard, the best place is a group that supports the
    particular compiler, i.e. news:gnu.gcc.*.

    > Thanks,
    > Nish.


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Oct 8, 2004
    #2
    1. Advertising

  3. Nish

    Mike Wahler Guest

    "Nish" <> wrote in message
    news:...
    > Hi,
    >
    > I have a question regarding inline functions in C. If I declare a
    > function as inline in the header file but do not define it there.
    > Then I define that function in .c file, however neither make it static
    > nor inline nor extern in the .c file.
    > What would the compiler (gcc) do in this case? Will it just create
    > object code for that function which can be called from any translation
    > unit and never inlined. Or will it create the object code for that
    > function, however inline it when I compile the .c file? Or neither of
    > these?
    > It would be great if someone could guide me to the correct group to
    > post this message, if this is not the right group.


    It's not. Here we discuss the language, not particular
    compilers. Try gnu.gcc or browse the http://gcc.gnu.org/
    site. Or perhaps ask in the gcc mailing list:
    http://gcc.gnu.org/lists.html

    As far as the C language is concerned, 'inline' is only
    treated as a request, which an implementation is free
    to grant or ignore.

    -Mike
     
    Mike Wahler, Oct 8, 2004
    #3
  4. Nish wrote:

    > I have a question regarding inline functions in C.
    > I declare a function as inline in the header file
    > but do not define it there.
    > Then I define that function in *.c file
    > [but] make it neither static or inline nor extern in the .c file.
    > What would the compiler (gcc) do in this case?


    > cat file.h

    #ifndef GUARD_FILE_H
    #define GUARD_FILE_H 1

    #include <stdio.h>

    inline void f(void);

    #endif//GUARD_FILE_H

    > cat file.c

    #include "file.h"

    void f(void) {
    fprintf(stdout, "f(void)\n");
    }

    > gcc -Wall -std=c99 -pedantic -c file.c
    > nm file.o

    00000000 T f
    U fprintf
    U stdout
    > cat main.c

    #include "file.h"

    int main(int argc, char* argv[]) {
    f();
    return 0;
    }

    > gcc -Wall -std=c99 -pedantic -o main main.c file.o
    > ./main

    f(void)

    > Will it just create object code for that function
    > which can be called from any translation unit and never inlined.


    Evidently.

    > Or will it create the object code for that function
    > [but] inline it when I compile the .c file?


    The function definition in file.c can't be inline'd
    when the function invocation is encountered in main.c
    because it isn't "visible" to gcc when it is compiling main.c

    > Or neither of these?
    > It would be great if someone could guide me
    > to the correct group to post this message


    gnu.gcc.help

    > if this is not the right group.
     
    E. Robert Tisdale, Oct 8, 2004
    #4
  5. Mike Wahler wrote:
    > "Nish" <> wrote in message
    > news:...
    >
    >>Hi,
    >>
    >>I have a question regarding inline functions in C. If I declare a
    >>function as inline in the header file but do not define it there.
    >>Then I define that function in .c file, however neither make it static
    >>nor inline nor extern in the .c file.


    > As far as the C language is concerned, 'inline' is only
    > treated as a request, which an implementation is free
    > to grant or ignore.
    >


    The described situation causes undefined behaviour though.

    --
    Thomas.
     
    Thomas Stegen, Oct 8, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TGOS
    Replies:
    3
    Views:
    384
    Kevin Bracey
    Feb 28, 2005
  2. Oodini
    Replies:
    1
    Views:
    1,795
    Keith Thompson
    Sep 27, 2005
  3. Travis
    Replies:
    6
    Views:
    549
    Markus Schoder
    Jun 28, 2007
  4. Mosfet
    Replies:
    4
    Views:
    271
    Juha Nieminen
    Oct 31, 2008
  5. Alok
    Replies:
    3
    Views:
    262
Loading...

Share This Page