1

I know inline keyword should be used only in header files, in order the compiler does not complain about more definition. I am talking about plain inline, no extern inline neither static inline, but when I make a function inline (in source file .c, then the compiler does not bother to provide the symbol at all:

#include <stdio.h>

inline void print(char *s){
    printf(s);
}

int main(){
    print("abc");
}

gives:

.text
    .section    .rodata
.LC0:
    .string "abc"
    .text
    .globl  main
    .type   main, @function
main:
    pushq   %rbp    #
    movq    %rsp, %rbp  #,
# a.c:8:    print("abc");
    leaq    .LC0(%rip), %rdi    #,
    call    print@PLT   #
    movl    $0, %eax    #, _3
# a.c:9: }
    popq    %rbp    #
    ret 
    .size   main, .-main
    .ident  "GCC: (Debian 8.3.0-6) 8.3.0"
    .section    .note.GNU-stack,"",@progbits

(No symbol for print function, thus no call print) and thus

/usr/bin/ld: /tmp/ccshH3yR.o: in function `main':
(.text+0xc): undefined reference to `print'
collect2: error: ld returned 1 exit status

But I do not see any correlation between enable more function definition (which is what inline keyword is for) and if it is in source, not header, does provided the symbol at all. I just do not understand, why had gcc decided for this behaviour. For those who think this is duplicate, I have already read all related questions: When should I write the keyword 'inline' for a function/method?, Inline function in header file in C, What is the use of the `inline` keyword in C?, and others. But no answer for why the compiler cut off the function at all (if in source).

milanHrabos
  • 2,010
  • 3
  • 11
  • 45
  • 1
    As indicated in the answers to the duplicate question, you just need to add `void print(char *s);` after the inline copy so that it can be instantiated as a function with external linkage. – Ian Abbott Aug 08 '20 at 16:06

0 Answers0