Skip to contentSkip to conversation

What is the use of extern inline?

DISCUSSION Shriramana Sharma 2 participants 3 messages May 29, 2007 – Jun 11, 2007

lore
Share & tools
lore
#0 lore raw

In man:gcc under -fkeep-inline-functions I first came across the usage of "extern inline". For normal functions, extern declares that the function exists elsewhere. But this does not seem to work for inline functions:

lib.cpp:
---------------------------------------
inline int foo ( void ) { return 2 ; }
---------------------------------------

main.cpp:
---------------------------------------

# include <cstdio>
extern inline int foo ( void ) ;
int main ( void ) {
	printf ( "%d\n", foo () ) ;
}
---------------------------------------
$ g++ -c main.cpp lib.cpp
main.cpp:3: warning: inline function ‘int foo()’ used but never defined
$ g++ -o main main.o lib.o
main.o: In function `main':
main.cpp:(.text+0x12): undefined reference to `foo()'
collect2: ld returned 1 exit status
$

nm on lib.o returned absolutely nothing! Apparently code for an inline function is not produced if it is never called. If it is called, and it is not labeled static inline, then it is both inlined and compiled separately. Strange, but ok.

So I tried adding a dummy function to lib.cpp calling foo() just to make it get compiled. Then compiling main.cpp gave the same warning as above, but linking and execution went on ok.

But I discovered that I don't need to have the inline keyword in the declaration of foo() in main.cpp. In fact, removing the inline keyword allows compilation without warning.

So what is the *unique* use of extern inline? I mean, where we cannot do without it?

Shriramana Sharma.

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#1 lore raw
Quoted 6 lines · Shriramana Sharma

In man:gcc under -fkeep-inline-functions I first came across the usage of "extern inline". For normal functions, extern declares that the function exists elsewhere. But this does not seem to work for inline functions:

lib.cpp:

Note that way in which "inline" is handled differs between C and C++.

Quoted 21 lines

---------------------------------------
inline int foo ( void ) { return 2 ; }
---------------------------------------

main.cpp:
---------------------------------------

# include <cstdio>
extern inline int foo ( void ) ;
int main ( void ) {
	printf ( "%d\n", foo () ) ;
}
---------------------------------------
$ g++ -c main.cpp lib.cpp
main.cpp:3: warning: inline function -F¡int foo()¢ used but never defined-A
$ g++ -o main main.o lib.o
main.o: In function `main':
main.cpp:(.text+0x12): undefined reference to `foo()'
collect2: ld returned 1 exit status
$
Quoted 2 lines

nm on lib.o returned absolutely nothing! Apparently code for an inline function is not produced if it is never called.

True for C++, not for C.

Quoted 8 lines

If it is called, and it
is not labeled static inline, then it is both inlined and compiled separately. Strange, but ok.

So I tried adding a dummy function to lib.cpp calling foo() just to make it get compiled. Then compiling main.cpp gave the same warning as above, but linking and execution went on ok.

Quoted 6 lines

But I discovered that I don't need to have the inline keyword in the declaration of foo() in main.cpp. In fact, removing the inline keyword allows compilation without warning.

So what is the *unique* use of extern inline? I mean, where we cannot do without it?

"extern inline" is meaningless. You can only inline functions which occur in the current translation unit.

Signature · 5 lines
Glynn Clements <[email protected]>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
Quoted 4 lines · Glynn Clements
Quoted 1 line

In man:gcc under -fkeep-inline-functions I first came across the usage of "extern inline". For normal functions, extern declares that the

"extern inline" is meaningless. You can only inline functions which occur in the current translation unit.

If this is so, then why does the GCC manpage have this usage?

Shriramana.