Function delcared inline but not defined inline

N

Nish

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.
 
J

Jack Klein

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 ( 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.
 
M

Mike Wahler

Nish said:
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
 
E

E. Robert Tisdale

Nish said:
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
 
T

Thomas Stegen

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top