I'm trying to compile the following code with clang at various optimization levels:
#include <stdio.h>
inline int foo() { return 42; }
int main() {
printf("%d\n", foo());
}
At -O1
, -O2
, -O3
, and -Os
, it compiles successfully, but it fails when using -O0
:
$ clang -O0 -o main main.c
Undefined symbols for architecture x86_64:
"_foo", referenced from:
_main in main-8b9319.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The failure at -O0
(and workarounds) can be explained by Clang's inline compatibility, but then I'd naively expect this to fail regardless of optimization level. It appears that some optimizations enabled at -O1
and above are doing something to prevent this link error from happening, but I'm curious as to which optimizations they are and why they seem to have different semantics than using inline
alone at -O0
.