Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Function delcared inline but not defined inline

Reply
Thread Tools

Function delcared inline but not defined inline

 
 
Nish
Guest
Posts: n/a
 
      10-08-2004
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.
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      10-08-2004
On 7 Oct 2004 18:07:11 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-08-2004

"Nish" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      10-08-2004
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.

 
Reply With Quote
 
Thomas Stegen
Guest
Posts: n/a
 
      10-08-2004
Mike Wahler wrote:
> "Nish" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
>
>>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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
solving circular dependencies with class only delcared in .h Mosfet C++ 4 10-31-2008 02:36 PM
Defined but not used? But I am using it! Travis C++ 6 06-28-2007 08:23 PM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
Use of inline function in inline function - is it allowed? TGOS C Programming 3 02-28-2005 10:49 AM



Advertisments