Inline not working properly in GCC?

J

James Douglas

I've copy-pasted the following from my terminal. I thought "inline"
functions were allowed to have multiple identical definitions, but GCC is
complaining.

$ cat f.h
inline void Func(void)
{

}

$ cat a.c
#include "f.h"

int main(void)
{
Func();

return 0;
}

$ cat b.c
#include "f.h"

void Hello(void)
{
Func();
}

$ gcc -std=c99 a.c b.c
/tmp/ccYf20Ik.o: In function `main':
a.c:(.text+0x12): undefined reference to `Func'
/tmp/ccsnIe6q.o: In function `Hello':
b.c:(.text+0x7): undefined reference to `Func'
collect2: ld returned 1 exit status

For now I'm using the following hack:

$ gcc -D inline=static -std=c99 a.c b.c
 
H

Huibert Bol

James said:
I've copy-pasted the following from my terminal. I thought "inline"
functions were allowed to have multiple identical definitions, but GCC is
complaining.

$ cat f.h
inline void Func(void)
{

}

$ cat a.c
#include "f.h"

int main(void)
{
Func();

return 0;
}

$ cat b.c
#include "f.h"

void Hello(void)
{
Func();
}

$ gcc -std=c99 a.c b.c
/tmp/ccYf20Ik.o: In function `main':
a.c:(.text+0x12): undefined reference to `Func'
/tmp/ccsnIe6q.o: In function `Hello':
b.c:(.text+0x7): undefined reference to `Func'
collect2: ld returned 1 exit status

Section 6.7.4 paragraph 6:

Any function with internal linkage can be an inline function. [...] If
all of the file scope declarations for a function in a translation unit
include the inline function specifier without extern, then the
definition in that translation unit is an inline definition.

[Func() is an inline definition]

An inline definition does not provide an external definition for the
function, and does not forbid an external definition in another
translation unit.

[Func() is *not* an external defintion]

[...] It is unspecified whether a call to the function uses the inline
definition or the external definition.

[GCC if free to use an external definition for Func(), which you
didn't supply]
 
K

Kaz Kylheku

J

J. J. Farrell

Trent said:
James said:
I've copy-pasted the following from my terminal. I thought "inline"
functions were allowed to have multiple identical definitions, but GCC is
complaining.
[snip]

This is because even with -std=c99 GCC doesn't comply with the C99 rules
for "inline" declarations.

http://www.greenend.org.uk/rjk/2003/03/inline.html

Are you sure? GCC 4.3 and later think they implement Standard 'inline'.
I'm sure they'd welcome a bug report if you think they're wrong.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top