I've been programming in C++ for a long time, and just ran into a very surprising situation in C.

$ cat x.c
inline int foo(void) {
return 42;

int main(void) {
return foo();

$ gcc x.c
undefined reference to 'foo'

$ gcc -O1 x.c
# no error

If you compile without optimizations, it makes sense if the `inline` keyword does nothing. But why is the function just being completely dropped? I guess I'll stop using `inline`. I just don't understand it.

· · Web · 1 · 0 · 2

@akkartik I've just read the explanation of exactly this case for return 42) and honestly I still have no idea what's going on here

@akkartik *3 hours later*

oh, probably I got it - the linker cant see a foo definition because its inline and inline functions are hidden from the linker

but the optimizer did not kick in yet and left the "foo" call inside main as a function call


@fedor Hilarious that I happened on the exact same example in the doc.

After reading your link I'm still confused, because my program has just a single translation unit. So I seem to be following the rules here.

"ISO C99 requires that exactly one C source file has the callable copy of the inline function."

"The program above will not link with the C99 inline semantics" I guess this means inline functions need to always be extern _somewhere_. Can't rely on inlining to occur.

Sign in to participate in the conversation

Revel in the marvels of the universe. We are a collective of forward-thinking individuals who strive to better ourselves and our surroundings through constant creation. We express ourselves through music, art, games, and writing. We also put great value in play. A warm welcome to any like-minded people who feel these ideals resonate with them.