Home > Enterprise >  Why do I get a linking error with clang when inlining a function in a C program?
Why do I get a linking error with clang when inlining a function in a C program?

Time:09-30

Why doesn't the following program compile with clang?

#include <stdio.h>

inline int f() {
  return 42;
}

int main() {
  printf("%d\n", f());
}

I get the following:

$ clang -o inline inline.c
Undefined symbols for architecture arm64:
  "_f", referenced from:
      _main in inline-975155.o
ld: symbol(s) not found for architecture arm64
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)

But I can compile it with clang just fine. Is there some nuance between inline in C vs C ?

CodePudding user response:

In C99 you need to provide an alternate (non-inline) definition of the function for when the compiler can't inline. See enter image description here

enter image description here https://godbolt.org/z/dh3G18PqK

CLANG works exactly the same way:

https://godbolt.org/z/ssMnzf5MG

https://godbolt.org/z/ssMnzf5MG

  • Related