I've read here:
and it stated:
A function defined with inline on its own. Stand-alone object code is always emitted. You can only write one definition like this in your entire program. If you want to use it from other translation units to the one where it is defined, you put a declaration in a header file; but it would not be inlined in those translation units.
however, in my minimal reproducible example:
test.c
inline
int foo(void)
{
return 0;
}
int main(void)
{
foo();
}
receives
cc -std=c99 -Wall -Wextra -Wshadow -Wpedantic test.c -o test
Undefined symbols for architecture x86_64:
"_foo", referenced from:
_main in test-febb67.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:4: test] Error 1
so it seems that code is not being properly emitted. The same problem is also present in gcc
. What is going on here? Specifically, I'd like to know, what are the differences here:
This fixes it.
inline foo(void) { /* definition */ }
foo(void);
This fixes it.
foo(void) { /* definition */ }
inline foo(void);
and my case: Wrong.
inline foo(void) { /* definition */ }
I saw here that it could be because it has external linkage, but an external definition is not provided; however, this is only references in main
and in a single translation unit (internally). Where is the symbol unresolved and why? It must be the call in main
.
I’ve found that with -O2
turned on, there is no issue, and this is because code is not originally being emitted and it is not being inlined by default.
This did not answer my question. I am not asking about the static inline
fix, except in why it is treated differently than just inline
in this case, which I still do not understand. Why does that always cause an emission?
I’d like to understand why this is a problem in the first place, since I’d read that it would emit code. Is this present anywhere explicitly in the standard?